All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v4 1/1] pci-host: add educational driver
@ 2015-01-21 16:28 Jiri Slaby
  2015-01-21 16:40 ` Paolo Bonzini
  0 siblings, 1 reply; 15+ messages in thread
From: Jiri Slaby @ 2015-01-21 16:28 UTC (permalink / raw)
  To: qemu-devel; +Cc: Paolo Bonzini, Jiri Slaby

I am using qemu for teaching the Linux kernel at our university. I
wrote a simple PCI device that can answer to writes/reads, generate
interrupts and perform DMA. As I am dragging it locally over 2 years,
I am sending it to you now.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
---
 MAINTAINERS             |   5 +
 default-configs/pci.mak |   1 +
 docs/specs/edu.txt      | 106 +++++++++++++
 hw/misc/Makefile.objs   |   1 +
 hw/misc/edu.c           | 401 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 514 insertions(+)
 create mode 100644 docs/specs/edu.txt
 create mode 100644 hw/misc/edu.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 430688dcab57..fd335a47bf5c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -599,6 +599,11 @@ F: hw/net/opencores_eth.c
 
 Devices
 -------
+EDU
+M: Jiri Slaby <jslaby@suse.cz>
+S: Maintained
+F: hw/misc/edu.c
+
 IDE
 M: Kevin Wolf <kwolf@redhat.com>
 M: Stefan Hajnoczi <stefanha@redhat.com>
diff --git a/default-configs/pci.mak b/default-configs/pci.mak
index a186c39c0e32..030cdc7d3dd0 100644
--- a/default-configs/pci.mak
+++ b/default-configs/pci.mak
@@ -32,3 +32,4 @@ CONFIG_PCI_TESTDEV=y
 CONFIG_NVME_PCI=y
 CONFIG_SD=y
 CONFIG_SDHCI=y
+CONFIG_EDU=y
diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt
new file mode 100644
index 000000000000..a15d6a2545f0
--- /dev/null
+++ b/docs/specs/edu.txt
@@ -0,0 +1,106 @@
+
+EDU device
+==========
+
+This is an educational device for writing (kernel) drivers. Its original
+intention was to support the Linux kernel lectures taught at the Masaryk
+University. Students are given this virtual device and are expected to write a
+driver with I/Os, IRQs, DMAs and such.
+
+The devices behaves very similar to the PCI bridge present in the COMBO6 cards
+developed under the Liberouter wings. Both PCI device ID and PCI space is
+inherited from that device.
+
+Command line switches:
+    -device edu[,dma_mask=mask]
+
+    dma_mask makes the virtual device work with DMA addresses with the given
+    mask. For educational purposes, the device supports only 28 bits (256 MiB)
+    by default. Students shall set dma_mask for the device in the OS driver
+    properly.
+
+PCI specs
+---------
+
+PCI ID: 1234:11e8
+
+PCI Region 0:
+   I/O memory, 1 MB in size. Users are supposed to communicate with the card
+   through this memory.
+
+MMIO area spec
+--------------
+
+Only size == 4 accesses are allowed for addresses < 0x80. size == 4 or
+size == 8 for the rest.
+
+0x00 (RO) : identification (0xRRrr00edu)
+	    RR -- major version
+	    rr -- minor version
+
+0x04 (RW) : card liveness check
+	    It is a simple value inversion (~ C operator).
+
+0x08 (RW) : factorial computation
+	    The stored value is taken and factorial of it is put back here.
+	    This happens only after factorial bit in the status register (0x20
+	    below) is cleared.
+
+0x20 (RW) : status register, bitwise OR
+	    0x01 -- computing factorial (RO)
+	    0x02 -- raise interrupt 0x01 after finishing factorial computation
+
+0x24 (RO) : interrupt status register
+	    It contains values which raised the interrupt (see interrupt raise
+	    register below).
+
+0x60 (WO) : interrupt raise register
+	    Raise an interrupt. The value will be put to the interrupt status
+	    register (using bitwise OR).
+
+0x64 (WO) : interrupt acknowledge register
+	    Clear an interrupt. The value will be cleared from the interrupt
+	    status register. This needs to be done from the ISR to stop
+	    generating interrupts.
+
+0x80 (RW) : DMA source address
+	    Where to perform the DMA from.
+
+0x88 (RW) : DMA destination address
+	    Where to perform the DMA to.
+
+0x90 (RW) : DMA transfer count
+	    The size of the area to perform the DMA on.
+
+0x98 (RW) : DMA command register, bitwise OR
+	    0x01 -- start transfer
+	    0x02 -- direction (0: from RAM to EDU, 1: from EDU to RAM)
+	    0x04 -- raise interrupt 0x100 after finishing the DMA
+
+IRQ controller
+--------------
+An IRQ is generated when written to the interrupt raise register. The value
+appears in interrupt status register when the interrupt is raised and has to
+be written to the interrupt acknowledge register to lower it.
+
+DMA controller
+--------------
+One has to specify, source, destination, size, and start the transfer. One
+4096 bytes long buffer at offset 0x40000 is available in the EDU device. I.e.
+one can perform DMA to/from this space when programmed properly.
+
+Example of transferring a 100 byte block to and from the buffer using a given
+PCI address 'addr':
+addr     -> DMA source address
+0x40000  -> DMA destination address
+100      -> DMA transfer count
+1        -> DMA command register
+while (DMA command register & 1)
+	;
+
+0x40000  -> DMA source address
+addr+100 -> DMA destination address
+100      -> DMA transfer count
+3        -> DMA command register
+while (DMA command register & 1)
+	;
diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs
index e47fea853065..029a56f279f1 100644
--- a/hw/misc/Makefile.objs
+++ b/hw/misc/Makefile.objs
@@ -40,3 +40,4 @@ obj-$(CONFIG_SLAVIO) += slavio_misc.o
 obj-$(CONFIG_ZYNQ) += zynq_slcr.o
 
 obj-$(CONFIG_PVPANIC) += pvpanic.o
+obj-$(CONFIG_EDU) += edu.o
diff --git a/hw/misc/edu.c b/hw/misc/edu.c
new file mode 100644
index 000000000000..c994a522bb33
--- /dev/null
+++ b/hw/misc/edu.c
@@ -0,0 +1,401 @@
+/*
+ * QEMU educational PCI device
+ *
+ * Copyright (c) 2012-2014 Jiri Slaby
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "hw/pci/pci.h"
+#include "qemu/timer.h"
+#include "qemu/main-loop.h" /* iothread mutex */
+#include "qapi/visitor.h"
+
+#define EDU(obj)        OBJECT_CHECK(EduState, obj, "edu")
+
+#define FACT_IRQ        0x00000001
+#define DMA_IRQ         0x00000100
+
+#define DMA_START       0x40000
+#define DMA_SIZE        4096
+
+typedef struct {
+    PCIDevice pdev;
+    MemoryRegion mmio;
+
+    QemuThread thread;
+    QemuMutex thr_mutex;
+    QemuCond thr_cond;
+    bool stopping;
+
+    uint32_t addr4;
+    uint32_t fact;
+#define EDU_STATUS_COMPUTING    0x01
+#define EDU_STATUS_IRQFACT      0x80
+    uint32_t status;
+
+    uint32_t irq_status;
+
+#define EDU_DMA_RUN             0x1
+#define EDU_DMA_DIR(cmd)        (((cmd) & 0x2) >> 1)
+# define EDU_DMA_FROM_PCI       0
+# define EDU_DMA_TO_PCI         1
+#define EDU_DMA_IRQ             0x4
+    struct dma_state {
+        dma_addr_t src;
+        dma_addr_t dst;
+        dma_addr_t cnt;
+        dma_addr_t cmd;
+    } dma;
+    QEMUTimer dma_timer;
+    char dma_buf[DMA_SIZE];
+    uint64_t dma_mask;
+} EduState;
+
+static void edu_raise_irq(EduState *edu, uint32_t val)
+{
+    edu->irq_status |= val;
+    if (edu->irq_status) {
+        pci_set_irq(&edu->pdev, 1);
+    }
+}
+
+static void edu_lower_irq(EduState *edu, uint32_t val)
+{
+    edu->irq_status &= ~val;
+
+    if (!edu->irq_status) {
+        pci_set_irq(&edu->pdev, 0);
+    }
+}
+
+static bool within(uint32_t addr, uint32_t start, uint32_t end)
+{
+    return start <= addr && addr < end;
+}
+
+static void edu_check_range(uint32_t addr, uint32_t size1, uint32_t start,
+                uint32_t size2)
+{
+    uint32_t end1 = addr + size1;
+    uint32_t end2 = start + size2;
+
+    if (within(addr, start, end2) &&
+            end1 > addr && within(end1, start, end2)) {
+        return;
+    }
+
+    hw_error("EDU: DMA range 0x%.8x-0x%.8x out of bounds (0x%.8x-0x%.8x)!",
+            addr, end1 - 1, start, end2 - 1);
+}
+
+static dma_addr_t edu_clamp_addr(const EduState *edu, dma_addr_t addr)
+{
+    dma_addr_t res = addr & edu->dma_mask;
+
+    if (addr != res) {
+        printf("EDU: clamping DMA 0x%.16lx to 0x%.16lx!\n", addr, res);
+    }
+
+    return res;
+}
+
+static void edu_dma_timer(void *opaque)
+{
+    EduState *edu = opaque;
+    bool raise_irq = false;
+
+    if (!(edu->dma.cmd & EDU_DMA_RUN)) {
+        return;
+    }
+
+    if (EDU_DMA_DIR(edu->dma.cmd) == EDU_DMA_FROM_PCI) {
+        uint32_t dst = edu->dma.dst;
+        edu_check_range(dst, edu->dma.cnt, DMA_START, DMA_SIZE);
+        dst -= DMA_START;
+        pci_dma_read(&edu->pdev, edu_clamp_addr(edu, edu->dma.src),
+                edu->dma_buf + dst, edu->dma.cnt);
+    } else {
+        uint32_t src = edu->dma.src;
+        edu_check_range(src, edu->dma.cnt, DMA_START, DMA_SIZE);
+        src -= DMA_START;
+        pci_dma_write(&edu->pdev, edu_clamp_addr(edu, edu->dma.dst),
+                edu->dma_buf + src, edu->dma.cnt);
+    }
+
+    edu->dma.cmd &= ~EDU_DMA_RUN;
+    if (edu->dma.cmd & EDU_DMA_IRQ) {
+        raise_irq = true;
+    }
+
+    if (raise_irq) {
+        edu_raise_irq(edu, DMA_IRQ);
+    }
+}
+
+static void dma_rw(EduState *edu, bool write, dma_addr_t *val, dma_addr_t *dma,
+                bool timer)
+{
+    if (write && (edu->dma.cmd & EDU_DMA_RUN)) {
+        return;
+    }
+
+    if (write) {
+        *dma = *val;
+    } else {
+        *val = *dma;
+    }
+
+    if (timer) {
+        timer_mod(&edu->dma_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 100);
+    }
+}
+
+static uint64_t edu_mmio_read(void *opaque, hwaddr addr, unsigned size)
+{
+    EduState *edu = opaque;
+    uint64_t val = ~0ULL;
+
+    if (size != 4) {
+        return val;
+    }
+
+    switch (addr) {
+    case 0x00:
+        val = 0x010000edu;
+        break;
+    case 0x04:
+        val = edu->addr4;
+        break;
+    case 0x08:
+        qemu_mutex_lock(&edu->thr_mutex);
+        val = edu->fact;
+        qemu_mutex_unlock(&edu->thr_mutex);
+        break;
+    case 0x20:
+        val = atomic_read(&edu->status);
+        break;
+    case 0x24:
+        val = edu->irq_status;
+        break;
+    case 0x80:
+        dma_rw(edu, false, &val, &edu->dma.src, false);
+        break;
+    case 0x88:
+        dma_rw(edu, false, &val, &edu->dma.dst, false);
+        break;
+    case 0x90:
+        dma_rw(edu, false, &val, &edu->dma.cnt, false);
+        break;
+    case 0x98:
+        dma_rw(edu, false, &val, &edu->dma.cmd, false);
+        break;
+    }
+
+    return val;
+}
+
+static void edu_mmio_write(void *opaque, hwaddr addr, uint64_t val,
+                unsigned size)
+{
+    EduState *edu = opaque;
+
+    if (addr < 0x80 && size != 4) {
+        return;
+    }
+
+    if (addr >= 0x80 && size != 4 && size != 8) {
+        return;
+    }
+
+    switch (addr) {
+    case 0x04:
+        edu->addr4 = ~val;
+        break;
+    case 0x08:
+        if (atomic_fetch_or(&edu->status, EDU_STATUS_COMPUTING) & EDU_STATUS_COMPUTING) {
+            break;
+        }
+        qemu_mutex_lock(&edu->thr_mutex);
+        edu->fact = val;
+        qemu_cond_signal(&edu->thr_cond);
+        qemu_mutex_unlock(&edu->thr_mutex);
+        break;
+    case 0x20:
+        if (val & EDU_STATUS_IRQFACT) {
+            atomic_or(&edu->status, EDU_STATUS_IRQFACT);
+        } else {
+            atomic_and(&edu->status, ~EDU_STATUS_IRQFACT);
+        }
+        break;
+    case 0x60:
+        edu_raise_irq(edu, val);
+        break;
+    case 0x64:
+        edu_lower_irq(edu, val);
+        break;
+    case 0x80:
+        dma_rw(edu, true, &val, &edu->dma.src, false);
+        break;
+    case 0x88:
+        dma_rw(edu, true, &val, &edu->dma.dst, false);
+        break;
+    case 0x90:
+        dma_rw(edu, true, &val, &edu->dma.cnt, false);
+        break;
+    case 0x98:
+        if (!(val & EDU_DMA_RUN)) {
+            break;
+        }
+        dma_rw(edu, true, &val, &edu->dma.cmd, true);
+        break;
+    }
+}
+
+static const MemoryRegionOps edu_mmio_ops = {
+    .read = edu_mmio_read,
+    .write = edu_mmio_write,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+/*
+ * We purposedly use a thread, so that users are forced to wait for the status
+ * register.
+ */
+static void *edu_fact_thread(void *opaque)
+{
+    EduState *edu = opaque;
+
+    while (1) {
+        uint32_t val, ret = 1;
+
+        qemu_mutex_lock(&edu->thr_mutex);
+        while ((atomic_read(&edu->status) & EDU_STATUS_COMPUTING) == 0 && !edu->stopping) {
+            qemu_cond_wait(&edu->thr_cond, &edu->thr_mutex);
+        }
+
+        if (edu->stopping) {
+            qemu_mutex_unlock(&edu->thr_mutex);
+            break;
+        }
+
+        val = edu->fact;
+        qemu_mutex_unlock(&edu->thr_mutex);
+
+        while (val > 0) {
+            ret *= val--;
+        }
+
+	/* we should sleep randomly here, so that students check the status properly */
+
+        qemu_mutex_lock(&edu->thr_mutex);
+        edu->fact = ret;
+        qemu_mutex_unlock(&edu->thr_mutex);
+        atomic_and(&edu->status, ~EDU_STATUS_COMPUTING);
+
+        if (atomic_read(&edu->status) & EDU_STATUS_IRQFACT) {
+            qemu_mutex_lock_iothread();
+            edu_raise_irq(edu, FACT_IRQ);
+            qemu_mutex_unlock_iothread();
+        }
+    }
+
+    return NULL;
+}
+
+static int pci_edu_init(PCIDevice *pdev)
+{
+    EduState *edu = DO_UPCAST(EduState, pdev, pdev);
+    uint8_t *pci_conf = pdev->config;
+
+    timer_init(&edu->dma_timer, main_loop_tlg.tl[QEMU_CLOCK_VIRTUAL], SCALE_MS,
+            edu_dma_timer, edu);
+
+    qemu_mutex_init(&edu->thr_mutex);
+    qemu_cond_init(&edu->thr_cond);
+    qemu_thread_create(&edu->thread, "edu", edu_fact_thread,
+                       edu, QEMU_THREAD_JOINABLE);
+
+    pci_config_set_interrupt_pin(pci_conf, 1);
+
+    memory_region_init_io(&edu->mmio, OBJECT(edu), &edu_mmio_ops, edu,
+                    "edu-mmio", 1 << 20);
+    pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &edu->mmio);
+
+    return 0;
+}
+
+static void pci_edu_uninit(PCIDevice *pdev)
+{
+    EduState *edu = DO_UPCAST(EduState, pdev, pdev);
+
+    qemu_mutex_lock(&edu->thr_mutex);
+    edu->stopping = true;
+    qemu_mutex_unlock(&edu->thr_mutex);
+    qemu_cond_signal(&edu->thr_cond);
+    qemu_thread_join(&edu->thread);
+
+    qemu_cond_destroy(&edu->thr_cond);
+    qemu_mutex_destroy(&edu->thr_mutex);
+
+    timer_del(&edu->dma_timer);
+}
+
+static void edu_obj_uint64(Object *obj, struct Visitor *v, void *opaque,
+                const char *name, Error **errp)
+{
+    uint64_t *val = opaque;
+
+    visit_type_uint64(v, val, name, errp);
+}
+
+static void edu_instance_init(Object *obj)
+{
+    EduState *edu = EDU(obj);
+
+    edu->dma_mask = (1UL << 28) - 1;
+    object_property_add(obj, "dma_mask", "uint64", edu_obj_uint64,
+                    edu_obj_uint64, NULL, &edu->dma_mask, NULL);
+}
+
+static void edu_class_init(ObjectClass *class, void *data)
+{
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(class);
+
+    k->init = pci_edu_init;
+    k->exit = pci_edu_uninit;
+    k->vendor_id = PCI_VENDOR_ID_QEMU;
+    k->device_id = 0x11e8;
+    k->revision = 0x10;
+    k->class_id = PCI_CLASS_OTHERS;
+}
+
+static void pci_edu_register_types(void)
+{
+    static const TypeInfo edu_info = {
+        .name          = "edu",
+        .parent        = TYPE_PCI_DEVICE,
+        .instance_size = sizeof(EduState),
+        .instance_init = edu_instance_init,
+        .class_init    = edu_class_init,
+    };
+
+    type_register_static(&edu_info);
+}
+type_init(pci_edu_register_types)
-- 
2.2.1

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH v4 1/1] pci-host: add educational driver
  2015-01-21 16:28 [Qemu-devel] [PATCH v4 1/1] pci-host: add educational driver Jiri Slaby
@ 2015-01-21 16:40 ` Paolo Bonzini
  2015-01-21 16:44   ` Jiri Slaby
  2015-01-21 16:48   ` [Qemu-devel] [PATCH v5 1/1] hw: misc, " Jiri Slaby
  0 siblings, 2 replies; 15+ messages in thread
From: Paolo Bonzini @ 2015-01-21 16:40 UTC (permalink / raw)
  To: Jiri Slaby, qemu-devel



On 21/01/2015 17:28, Jiri Slaby wrote:
> +        if (atomic_fetch_or(&edu->status, EDU_STATUS_COMPUTING) & EDU_STATUS_COMPUTING) {

Theoretically the other thread could see EDU_STATUS_COMPUTING here and
not enter the condvar wait.  So...

> +            break;
> +        }
> +        qemu_mutex_lock(&edu->thr_mutex);
> +        edu->fact = val;
> +        qemu_cond_signal(&edu->thr_cond);
> +        qemu_mutex_unlock(&edu->thr_mutex);

... just one change:

   if (atomic_read(&edu->status) & EDU_STATUS_COMPUTING) {
       break;
   }
   /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because
    * it is only set in this function and it is under the iothread
    * mutex.
    */
   qemu_mutex_lock(&edu->thr_mutex);
   edu->fact = val;
   atomic_or(&edu->status), EDU_STATUS_COMPUTING);
   qemu_cond_signal(&edu->thr_cond);
   qemu_mutex_unlock(&edu->thr_mutex);

If you are okay with this change, I'll apply the patch to my tree.

Paolo

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH v4 1/1] pci-host: add educational driver
  2015-01-21 16:40 ` Paolo Bonzini
@ 2015-01-21 16:44   ` Jiri Slaby
  2015-01-21 16:48   ` [Qemu-devel] [PATCH v5 1/1] hw: misc, " Jiri Slaby
  1 sibling, 0 replies; 15+ messages in thread
From: Jiri Slaby @ 2015-01-21 16:44 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel

On 01/21/2015, 05:40 PM, Paolo Bonzini wrote:
> 
> 
> On 21/01/2015 17:28, Jiri Slaby wrote:
>> +        if (atomic_fetch_or(&edu->status, EDU_STATUS_COMPUTING) & EDU_STATUS_COMPUTING) {
> 
> Theoretically the other thread could see EDU_STATUS_COMPUTING here and
> not enter the condvar wait.  So...
> 
>> +            break;
>> +        }
>> +        qemu_mutex_lock(&edu->thr_mutex);
>> +        edu->fact = val;
>> +        qemu_cond_signal(&edu->thr_cond);
>> +        qemu_mutex_unlock(&edu->thr_mutex);
> 
> ... just one change:
> 
>    if (atomic_read(&edu->status) & EDU_STATUS_COMPUTING) {
>        break;
>    }
>    /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because
>     * it is only set in this function and it is under the iothread
>     * mutex.
>     */
>    qemu_mutex_lock(&edu->thr_mutex);
>    edu->fact = val;
>    atomic_or(&edu->status), EDU_STATUS_COMPUTING);
>    qemu_cond_signal(&edu->thr_cond);
>    qemu_mutex_unlock(&edu->thr_mutex);
> 
> If you are okay with this change, I'll apply the patch to my tree.

Oh, I see. I will send v5 shortly as I found a bug in documentation too.

Thanks a lot for the review.

-- 
js
suse labs

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Qemu-devel] [PATCH v5 1/1] hw: misc, add educational driver
  2015-01-21 16:40 ` Paolo Bonzini
  2015-01-21 16:44   ` Jiri Slaby
@ 2015-01-21 16:48   ` Jiri Slaby
  2015-01-21 16:58     ` Paolo Bonzini
                       ` (2 more replies)
  1 sibling, 3 replies; 15+ messages in thread
From: Jiri Slaby @ 2015-01-21 16:48 UTC (permalink / raw)
  To: qemu-devel; +Cc: Paolo Bonzini, Jiri Slaby

I am using qemu for teaching the Linux kernel at our university. I
wrote a simple PCI device that can answer to writes/reads, generate
interrupts and perform DMA. As I am dragging it locally over 2 years,
I am sending it to you now.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
---
 MAINTAINERS             |   5 +
 default-configs/pci.mak |   1 +
 docs/specs/edu.txt      | 106 +++++++++++++
 hw/misc/Makefile.objs   |   1 +
 hw/misc/edu.c           | 409 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 522 insertions(+)
 create mode 100644 docs/specs/edu.txt
 create mode 100644 hw/misc/edu.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 430688dcab57..fd335a47bf5c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -599,6 +599,11 @@ F: hw/net/opencores_eth.c
 
 Devices
 -------
+EDU
+M: Jiri Slaby <jslaby@suse.cz>
+S: Maintained
+F: hw/misc/edu.c
+
 IDE
 M: Kevin Wolf <kwolf@redhat.com>
 M: Stefan Hajnoczi <stefanha@redhat.com>
diff --git a/default-configs/pci.mak b/default-configs/pci.mak
index a186c39c0e32..030cdc7d3dd0 100644
--- a/default-configs/pci.mak
+++ b/default-configs/pci.mak
@@ -32,3 +32,4 @@ CONFIG_PCI_TESTDEV=y
 CONFIG_NVME_PCI=y
 CONFIG_SD=y
 CONFIG_SDHCI=y
+CONFIG_EDU=y
diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt
new file mode 100644
index 000000000000..360af27ec8b1
--- /dev/null
+++ b/docs/specs/edu.txt
@@ -0,0 +1,106 @@
+
+EDU device
+==========
+
+This is an educational device for writing (kernel) drivers. Its original
+intention was to support the Linux kernel lectures taught at the Masaryk
+University. Students are given this virtual device and are expected to write a
+driver with I/Os, IRQs, DMAs and such.
+
+The devices behaves very similar to the PCI bridge present in the COMBO6 cards
+developed under the Liberouter wings. Both PCI device ID and PCI space is
+inherited from that device.
+
+Command line switches:
+    -device edu[,dma_mask=mask]
+
+    dma_mask makes the virtual device work with DMA addresses with the given
+    mask. For educational purposes, the device supports only 28 bits (256 MiB)
+    by default. Students shall set dma_mask for the device in the OS driver
+    properly.
+
+PCI specs
+---------
+
+PCI ID: 1234:11e8
+
+PCI Region 0:
+   I/O memory, 1 MB in size. Users are supposed to communicate with the card
+   through this memory.
+
+MMIO area spec
+--------------
+
+Only size == 4 accesses are allowed for addresses < 0x80. size == 4 or
+size == 8 for the rest.
+
+0x00 (RO) : identification (0xRRrr00edu)
+	    RR -- major version
+	    rr -- minor version
+
+0x04 (RW) : card liveness check
+	    It is a simple value inversion (~ C operator).
+
+0x08 (RW) : factorial computation
+	    The stored value is taken and factorial of it is put back here.
+	    This happens only after factorial bit in the status register (0x20
+	    below) is cleared.
+
+0x20 (RW) : status register, bitwise OR
+	    0x01 -- computing factorial (RO)
+	    0x80 -- raise interrupt 0x01 after finishing factorial computation
+
+0x24 (RO) : interrupt status register
+	    It contains values which raised the interrupt (see interrupt raise
+	    register below).
+
+0x60 (WO) : interrupt raise register
+	    Raise an interrupt. The value will be put to the interrupt status
+	    register (using bitwise OR).
+
+0x64 (WO) : interrupt acknowledge register
+	    Clear an interrupt. The value will be cleared from the interrupt
+	    status register. This needs to be done from the ISR to stop
+	    generating interrupts.
+
+0x80 (RW) : DMA source address
+	    Where to perform the DMA from.
+
+0x88 (RW) : DMA destination address
+	    Where to perform the DMA to.
+
+0x90 (RW) : DMA transfer count
+	    The size of the area to perform the DMA on.
+
+0x98 (RW) : DMA command register, bitwise OR
+	    0x01 -- start transfer
+	    0x02 -- direction (0: from RAM to EDU, 1: from EDU to RAM)
+	    0x04 -- raise interrupt 0x100 after finishing the DMA
+
+IRQ controller
+--------------
+An IRQ is generated when written to the interrupt raise register. The value
+appears in interrupt status register when the interrupt is raised and has to
+be written to the interrupt acknowledge register to lower it.
+
+DMA controller
+--------------
+One has to specify, source, destination, size, and start the transfer. One
+4096 bytes long buffer at offset 0x40000 is available in the EDU device. I.e.
+one can perform DMA to/from this space when programmed properly.
+
+Example of transferring a 100 byte block to and from the buffer using a given
+PCI address 'addr':
+addr     -> DMA source address
+0x40000  -> DMA destination address
+100      -> DMA transfer count
+1        -> DMA command register
+while (DMA command register & 1)
+	;
+
+0x40000  -> DMA source address
+addr+100 -> DMA destination address
+100      -> DMA transfer count
+3        -> DMA command register
+while (DMA command register & 1)
+	;
diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs
index e47fea853065..029a56f279f1 100644
--- a/hw/misc/Makefile.objs
+++ b/hw/misc/Makefile.objs
@@ -40,3 +40,4 @@ obj-$(CONFIG_SLAVIO) += slavio_misc.o
 obj-$(CONFIG_ZYNQ) += zynq_slcr.o
 
 obj-$(CONFIG_PVPANIC) += pvpanic.o
+obj-$(CONFIG_EDU) += edu.o
diff --git a/hw/misc/edu.c b/hw/misc/edu.c
new file mode 100644
index 000000000000..c74f9b64540d
--- /dev/null
+++ b/hw/misc/edu.c
@@ -0,0 +1,409 @@
+/*
+ * QEMU educational PCI device
+ *
+ * Copyright (c) 2012-2014 Jiri Slaby
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "hw/pci/pci.h"
+#include "qemu/timer.h"
+#include "qemu/main-loop.h" /* iothread mutex */
+#include "qapi/visitor.h"
+
+#define EDU(obj)        OBJECT_CHECK(EduState, obj, "edu")
+
+#define FACT_IRQ        0x00000001
+#define DMA_IRQ         0x00000100
+
+#define DMA_START       0x40000
+#define DMA_SIZE        4096
+
+typedef struct {
+    PCIDevice pdev;
+    MemoryRegion mmio;
+
+    QemuThread thread;
+    QemuMutex thr_mutex;
+    QemuCond thr_cond;
+    bool stopping;
+
+    uint32_t addr4;
+    uint32_t fact;
+#define EDU_STATUS_COMPUTING    0x01
+#define EDU_STATUS_IRQFACT      0x80
+    uint32_t status;
+
+    uint32_t irq_status;
+
+#define EDU_DMA_RUN             0x1
+#define EDU_DMA_DIR(cmd)        (((cmd) & 0x2) >> 1)
+# define EDU_DMA_FROM_PCI       0
+# define EDU_DMA_TO_PCI         1
+#define EDU_DMA_IRQ             0x4
+    struct dma_state {
+        dma_addr_t src;
+        dma_addr_t dst;
+        dma_addr_t cnt;
+        dma_addr_t cmd;
+    } dma;
+    QEMUTimer dma_timer;
+    char dma_buf[DMA_SIZE];
+    uint64_t dma_mask;
+} EduState;
+
+static void edu_raise_irq(EduState *edu, uint32_t val)
+{
+    edu->irq_status |= val;
+    if (edu->irq_status) {
+        pci_set_irq(&edu->pdev, 1);
+    }
+}
+
+static void edu_lower_irq(EduState *edu, uint32_t val)
+{
+    edu->irq_status &= ~val;
+
+    if (!edu->irq_status) {
+        pci_set_irq(&edu->pdev, 0);
+    }
+}
+
+static bool within(uint32_t addr, uint32_t start, uint32_t end)
+{
+    return start <= addr && addr < end;
+}
+
+static void edu_check_range(uint32_t addr, uint32_t size1, uint32_t start,
+                uint32_t size2)
+{
+    uint32_t end1 = addr + size1;
+    uint32_t end2 = start + size2;
+
+    if (within(addr, start, end2) &&
+            end1 > addr && within(end1, start, end2)) {
+        return;
+    }
+
+    hw_error("EDU: DMA range 0x%.8x-0x%.8x out of bounds (0x%.8x-0x%.8x)!",
+            addr, end1 - 1, start, end2 - 1);
+}
+
+static dma_addr_t edu_clamp_addr(const EduState *edu, dma_addr_t addr)
+{
+    dma_addr_t res = addr & edu->dma_mask;
+
+    if (addr != res) {
+        printf("EDU: clamping DMA 0x%.16lx to 0x%.16lx!\n", addr, res);
+    }
+
+    return res;
+}
+
+static void edu_dma_timer(void *opaque)
+{
+    EduState *edu = opaque;
+    bool raise_irq = false;
+
+    if (!(edu->dma.cmd & EDU_DMA_RUN)) {
+        return;
+    }
+
+    if (EDU_DMA_DIR(edu->dma.cmd) == EDU_DMA_FROM_PCI) {
+        uint32_t dst = edu->dma.dst;
+        edu_check_range(dst, edu->dma.cnt, DMA_START, DMA_SIZE);
+        dst -= DMA_START;
+        pci_dma_read(&edu->pdev, edu_clamp_addr(edu, edu->dma.src),
+                edu->dma_buf + dst, edu->dma.cnt);
+    } else {
+        uint32_t src = edu->dma.src;
+        edu_check_range(src, edu->dma.cnt, DMA_START, DMA_SIZE);
+        src -= DMA_START;
+        pci_dma_write(&edu->pdev, edu_clamp_addr(edu, edu->dma.dst),
+                edu->dma_buf + src, edu->dma.cnt);
+    }
+
+    edu->dma.cmd &= ~EDU_DMA_RUN;
+    if (edu->dma.cmd & EDU_DMA_IRQ) {
+        raise_irq = true;
+    }
+
+    if (raise_irq) {
+        edu_raise_irq(edu, DMA_IRQ);
+    }
+}
+
+static void dma_rw(EduState *edu, bool write, dma_addr_t *val, dma_addr_t *dma,
+                bool timer)
+{
+    if (write && (edu->dma.cmd & EDU_DMA_RUN)) {
+        return;
+    }
+
+    if (write) {
+        *dma = *val;
+    } else {
+        *val = *dma;
+    }
+
+    if (timer) {
+        timer_mod(&edu->dma_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 100);
+    }
+}
+
+static uint64_t edu_mmio_read(void *opaque, hwaddr addr, unsigned size)
+{
+    EduState *edu = opaque;
+    uint64_t val = ~0ULL;
+
+    if (size != 4) {
+        return val;
+    }
+
+    switch (addr) {
+    case 0x00:
+        val = 0x010000edu;
+        break;
+    case 0x04:
+        val = edu->addr4;
+        break;
+    case 0x08:
+        qemu_mutex_lock(&edu->thr_mutex);
+        val = edu->fact;
+        qemu_mutex_unlock(&edu->thr_mutex);
+        break;
+    case 0x20:
+        val = atomic_read(&edu->status);
+        break;
+    case 0x24:
+        val = edu->irq_status;
+        break;
+    case 0x80:
+        dma_rw(edu, false, &val, &edu->dma.src, false);
+        break;
+    case 0x88:
+        dma_rw(edu, false, &val, &edu->dma.dst, false);
+        break;
+    case 0x90:
+        dma_rw(edu, false, &val, &edu->dma.cnt, false);
+        break;
+    case 0x98:
+        dma_rw(edu, false, &val, &edu->dma.cmd, false);
+        break;
+    }
+
+    return val;
+}
+
+static void edu_mmio_write(void *opaque, hwaddr addr, uint64_t val,
+                unsigned size)
+{
+    EduState *edu = opaque;
+
+    if (addr < 0x80 && size != 4) {
+        return;
+    }
+
+    if (addr >= 0x80 && size != 4 && size != 8) {
+        return;
+    }
+
+    switch (addr) {
+    case 0x04:
+        edu->addr4 = ~val;
+        break;
+    case 0x08:
+        if (atomic_read(&edu->status) & EDU_STATUS_COMPUTING) {
+            break;
+        }
+        /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because it is only
+         * set in this function and it is under the iothread mutex.
+         */
+        qemu_mutex_lock(&edu->thr_mutex);
+        edu->fact = val;
+        atomic_or(&edu->status, EDU_STATUS_COMPUTING);
+        qemu_cond_signal(&edu->thr_cond);
+        qemu_mutex_unlock(&edu->thr_mutex);
+        break;
+    case 0x20:
+        if (val & EDU_STATUS_IRQFACT) {
+            atomic_or(&edu->status, EDU_STATUS_IRQFACT);
+        } else {
+            atomic_and(&edu->status, ~EDU_STATUS_IRQFACT);
+        }
+        break;
+    case 0x60:
+        edu_raise_irq(edu, val);
+        break;
+    case 0x64:
+        edu_lower_irq(edu, val);
+        break;
+    case 0x80:
+        dma_rw(edu, true, &val, &edu->dma.src, false);
+        break;
+    case 0x88:
+        dma_rw(edu, true, &val, &edu->dma.dst, false);
+        break;
+    case 0x90:
+        dma_rw(edu, true, &val, &edu->dma.cnt, false);
+        break;
+    case 0x98:
+        if (!(val & EDU_DMA_RUN)) {
+            break;
+        }
+        dma_rw(edu, true, &val, &edu->dma.cmd, true);
+        break;
+    }
+}
+
+static const MemoryRegionOps edu_mmio_ops = {
+    .read = edu_mmio_read,
+    .write = edu_mmio_write,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+/*
+ * We purposedly use a thread, so that users are forced to wait for the status
+ * register.
+ */
+static void *edu_fact_thread(void *opaque)
+{
+    EduState *edu = opaque;
+
+    while (1) {
+        uint32_t val, ret = 1;
+
+        qemu_mutex_lock(&edu->thr_mutex);
+        while ((atomic_read(&edu->status) & EDU_STATUS_COMPUTING) == 0 &&
+                        !edu->stopping) {
+            qemu_cond_wait(&edu->thr_cond, &edu->thr_mutex);
+        }
+
+        if (edu->stopping) {
+            qemu_mutex_unlock(&edu->thr_mutex);
+            break;
+        }
+
+        val = edu->fact;
+        qemu_mutex_unlock(&edu->thr_mutex);
+
+        while (val > 0) {
+            ret *= val--;
+        }
+
+        /*
+         * We should sleep for a random period here, so that students are
+         * forced to check the status properly.
+         */
+
+        qemu_mutex_lock(&edu->thr_mutex);
+        edu->fact = ret;
+        qemu_mutex_unlock(&edu->thr_mutex);
+        atomic_and(&edu->status, ~EDU_STATUS_COMPUTING);
+
+        if (atomic_read(&edu->status) & EDU_STATUS_IRQFACT) {
+            qemu_mutex_lock_iothread();
+            edu_raise_irq(edu, FACT_IRQ);
+            qemu_mutex_unlock_iothread();
+        }
+    }
+
+    return NULL;
+}
+
+static int pci_edu_init(PCIDevice *pdev)
+{
+    EduState *edu = DO_UPCAST(EduState, pdev, pdev);
+    uint8_t *pci_conf = pdev->config;
+
+    timer_init(&edu->dma_timer, main_loop_tlg.tl[QEMU_CLOCK_VIRTUAL], SCALE_MS,
+            edu_dma_timer, edu);
+
+    qemu_mutex_init(&edu->thr_mutex);
+    qemu_cond_init(&edu->thr_cond);
+    qemu_thread_create(&edu->thread, "edu", edu_fact_thread,
+                       edu, QEMU_THREAD_JOINABLE);
+
+    pci_config_set_interrupt_pin(pci_conf, 1);
+
+    memory_region_init_io(&edu->mmio, OBJECT(edu), &edu_mmio_ops, edu,
+                    "edu-mmio", 1 << 20);
+    pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &edu->mmio);
+
+    return 0;
+}
+
+static void pci_edu_uninit(PCIDevice *pdev)
+{
+    EduState *edu = DO_UPCAST(EduState, pdev, pdev);
+
+    qemu_mutex_lock(&edu->thr_mutex);
+    edu->stopping = true;
+    qemu_mutex_unlock(&edu->thr_mutex);
+    qemu_cond_signal(&edu->thr_cond);
+    qemu_thread_join(&edu->thread);
+
+    qemu_cond_destroy(&edu->thr_cond);
+    qemu_mutex_destroy(&edu->thr_mutex);
+
+    timer_del(&edu->dma_timer);
+}
+
+static void edu_obj_uint64(Object *obj, struct Visitor *v, void *opaque,
+                const char *name, Error **errp)
+{
+    uint64_t *val = opaque;
+
+    visit_type_uint64(v, val, name, errp);
+}
+
+static void edu_instance_init(Object *obj)
+{
+    EduState *edu = EDU(obj);
+
+    edu->dma_mask = (1UL << 28) - 1;
+    object_property_add(obj, "dma_mask", "uint64", edu_obj_uint64,
+                    edu_obj_uint64, NULL, &edu->dma_mask, NULL);
+}
+
+static void edu_class_init(ObjectClass *class, void *data)
+{
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(class);
+
+    k->init = pci_edu_init;
+    k->exit = pci_edu_uninit;
+    k->vendor_id = PCI_VENDOR_ID_QEMU;
+    k->device_id = 0x11e8;
+    k->revision = 0x10;
+    k->class_id = PCI_CLASS_OTHERS;
+}
+
+static void pci_edu_register_types(void)
+{
+    static const TypeInfo edu_info = {
+        .name          = "edu",
+        .parent        = TYPE_PCI_DEVICE,
+        .instance_size = sizeof(EduState),
+        .instance_init = edu_instance_init,
+        .class_init    = edu_class_init,
+    };
+
+    type_register_static(&edu_info);
+}
+type_init(pci_edu_register_types)
-- 
2.2.1

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH v5 1/1] hw: misc, add educational driver
  2015-01-21 16:48   ` [Qemu-devel] [PATCH v5 1/1] hw: misc, " Jiri Slaby
@ 2015-01-21 16:58     ` Paolo Bonzini
  2015-01-21 23:37     ` Eric Blake
  2015-01-21 23:49     ` [Qemu-devel] [PATCH v5 1/1] hw: misc, add educational driver Eric Blake
  2 siblings, 0 replies; 15+ messages in thread
From: Paolo Bonzini @ 2015-01-21 16:58 UTC (permalink / raw)
  To: Jiri Slaby, qemu-devel



On 21/01/2015 17:48, Jiri Slaby wrote:
> I am using qemu for teaching the Linux kernel at our university. I
> wrote a simple PCI device that can answer to writes/reads, generate
> interrupts and perform DMA. As I am dragging it locally over 2 years,
> I am sending it to you now.
> 
> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
> ---
>  MAINTAINERS             |   5 +
>  default-configs/pci.mak |   1 +
>  docs/specs/edu.txt      | 106 +++++++++++++
>  hw/misc/Makefile.objs   |   1 +
>  hw/misc/edu.c           | 409 ++++++++++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 522 insertions(+)
>  create mode 100644 docs/specs/edu.txt
>  create mode 100644 hw/misc/edu.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 430688dcab57..fd335a47bf5c 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -599,6 +599,11 @@ F: hw/net/opencores_eth.c
>  
>  Devices
>  -------
> +EDU
> +M: Jiri Slaby <jslaby@suse.cz>
> +S: Maintained
> +F: hw/misc/edu.c
> +
>  IDE
>  M: Kevin Wolf <kwolf@redhat.com>
>  M: Stefan Hajnoczi <stefanha@redhat.com>
> diff --git a/default-configs/pci.mak b/default-configs/pci.mak
> index a186c39c0e32..030cdc7d3dd0 100644
> --- a/default-configs/pci.mak
> +++ b/default-configs/pci.mak
> @@ -32,3 +32,4 @@ CONFIG_PCI_TESTDEV=y
>  CONFIG_NVME_PCI=y
>  CONFIG_SD=y
>  CONFIG_SDHCI=y
> +CONFIG_EDU=y
> diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt
> new file mode 100644
> index 000000000000..360af27ec8b1
> --- /dev/null
> +++ b/docs/specs/edu.txt
> @@ -0,0 +1,106 @@
> +
> +EDU device
> +==========
> +
> +This is an educational device for writing (kernel) drivers. Its original
> +intention was to support the Linux kernel lectures taught at the Masaryk
> +University. Students are given this virtual device and are expected to write a
> +driver with I/Os, IRQs, DMAs and such.
> +
> +The devices behaves very similar to the PCI bridge present in the COMBO6 cards
> +developed under the Liberouter wings. Both PCI device ID and PCI space is
> +inherited from that device.
> +
> +Command line switches:
> +    -device edu[,dma_mask=mask]
> +
> +    dma_mask makes the virtual device work with DMA addresses with the given
> +    mask. For educational purposes, the device supports only 28 bits (256 MiB)
> +    by default. Students shall set dma_mask for the device in the OS driver
> +    properly.
> +
> +PCI specs
> +---------
> +
> +PCI ID: 1234:11e8
> +
> +PCI Region 0:
> +   I/O memory, 1 MB in size. Users are supposed to communicate with the card
> +   through this memory.
> +
> +MMIO area spec
> +--------------
> +
> +Only size == 4 accesses are allowed for addresses < 0x80. size == 4 or
> +size == 8 for the rest.
> +
> +0x00 (RO) : identification (0xRRrr00edu)
> +	    RR -- major version
> +	    rr -- minor version
> +
> +0x04 (RW) : card liveness check
> +	    It is a simple value inversion (~ C operator).
> +
> +0x08 (RW) : factorial computation
> +	    The stored value is taken and factorial of it is put back here.
> +	    This happens only after factorial bit in the status register (0x20
> +	    below) is cleared.
> +
> +0x20 (RW) : status register, bitwise OR
> +	    0x01 -- computing factorial (RO)
> +	    0x80 -- raise interrupt 0x01 after finishing factorial computation
> +
> +0x24 (RO) : interrupt status register
> +	    It contains values which raised the interrupt (see interrupt raise
> +	    register below).
> +
> +0x60 (WO) : interrupt raise register
> +	    Raise an interrupt. The value will be put to the interrupt status
> +	    register (using bitwise OR).
> +
> +0x64 (WO) : interrupt acknowledge register
> +	    Clear an interrupt. The value will be cleared from the interrupt
> +	    status register. This needs to be done from the ISR to stop
> +	    generating interrupts.
> +
> +0x80 (RW) : DMA source address
> +	    Where to perform the DMA from.
> +
> +0x88 (RW) : DMA destination address
> +	    Where to perform the DMA to.
> +
> +0x90 (RW) : DMA transfer count
> +	    The size of the area to perform the DMA on.
> +
> +0x98 (RW) : DMA command register, bitwise OR
> +	    0x01 -- start transfer
> +	    0x02 -- direction (0: from RAM to EDU, 1: from EDU to RAM)
> +	    0x04 -- raise interrupt 0x100 after finishing the DMA
> +
> +IRQ controller
> +--------------
> +An IRQ is generated when written to the interrupt raise register. The value
> +appears in interrupt status register when the interrupt is raised and has to
> +be written to the interrupt acknowledge register to lower it.
> +
> +DMA controller
> +--------------
> +One has to specify, source, destination, size, and start the transfer. One
> +4096 bytes long buffer at offset 0x40000 is available in the EDU device. I.e.
> +one can perform DMA to/from this space when programmed properly.
> +
> +Example of transferring a 100 byte block to and from the buffer using a given
> +PCI address 'addr':
> +addr     -> DMA source address
> +0x40000  -> DMA destination address
> +100      -> DMA transfer count
> +1        -> DMA command register
> +while (DMA command register & 1)
> +	;
> +
> +0x40000  -> DMA source address
> +addr+100 -> DMA destination address
> +100      -> DMA transfer count
> +3        -> DMA command register
> +while (DMA command register & 1)
> +	;
> diff --git a/hw/misc/Makefile.objs b/hw/misc/Makefile.objs
> index e47fea853065..029a56f279f1 100644
> --- a/hw/misc/Makefile.objs
> +++ b/hw/misc/Makefile.objs
> @@ -40,3 +40,4 @@ obj-$(CONFIG_SLAVIO) += slavio_misc.o
>  obj-$(CONFIG_ZYNQ) += zynq_slcr.o
>  
>  obj-$(CONFIG_PVPANIC) += pvpanic.o
> +obj-$(CONFIG_EDU) += edu.o
> diff --git a/hw/misc/edu.c b/hw/misc/edu.c
> new file mode 100644
> index 000000000000..c74f9b64540d
> --- /dev/null
> +++ b/hw/misc/edu.c
> @@ -0,0 +1,409 @@
> +/*
> + * QEMU educational PCI device
> + *
> + * Copyright (c) 2012-2014 Jiri Slaby
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
> + * DEALINGS IN THE SOFTWARE.
> + */
> +
> +#include "hw/pci/pci.h"
> +#include "qemu/timer.h"
> +#include "qemu/main-loop.h" /* iothread mutex */
> +#include "qapi/visitor.h"
> +
> +#define EDU(obj)        OBJECT_CHECK(EduState, obj, "edu")
> +
> +#define FACT_IRQ        0x00000001
> +#define DMA_IRQ         0x00000100
> +
> +#define DMA_START       0x40000
> +#define DMA_SIZE        4096
> +
> +typedef struct {
> +    PCIDevice pdev;
> +    MemoryRegion mmio;
> +
> +    QemuThread thread;
> +    QemuMutex thr_mutex;
> +    QemuCond thr_cond;
> +    bool stopping;
> +
> +    uint32_t addr4;
> +    uint32_t fact;
> +#define EDU_STATUS_COMPUTING    0x01
> +#define EDU_STATUS_IRQFACT      0x80
> +    uint32_t status;
> +
> +    uint32_t irq_status;
> +
> +#define EDU_DMA_RUN             0x1
> +#define EDU_DMA_DIR(cmd)        (((cmd) & 0x2) >> 1)
> +# define EDU_DMA_FROM_PCI       0
> +# define EDU_DMA_TO_PCI         1
> +#define EDU_DMA_IRQ             0x4
> +    struct dma_state {
> +        dma_addr_t src;
> +        dma_addr_t dst;
> +        dma_addr_t cnt;
> +        dma_addr_t cmd;
> +    } dma;
> +    QEMUTimer dma_timer;
> +    char dma_buf[DMA_SIZE];
> +    uint64_t dma_mask;
> +} EduState;
> +
> +static void edu_raise_irq(EduState *edu, uint32_t val)
> +{
> +    edu->irq_status |= val;
> +    if (edu->irq_status) {
> +        pci_set_irq(&edu->pdev, 1);
> +    }
> +}
> +
> +static void edu_lower_irq(EduState *edu, uint32_t val)
> +{
> +    edu->irq_status &= ~val;
> +
> +    if (!edu->irq_status) {
> +        pci_set_irq(&edu->pdev, 0);
> +    }
> +}
> +
> +static bool within(uint32_t addr, uint32_t start, uint32_t end)
> +{
> +    return start <= addr && addr < end;
> +}
> +
> +static void edu_check_range(uint32_t addr, uint32_t size1, uint32_t start,
> +                uint32_t size2)
> +{
> +    uint32_t end1 = addr + size1;
> +    uint32_t end2 = start + size2;
> +
> +    if (within(addr, start, end2) &&
> +            end1 > addr && within(end1, start, end2)) {
> +        return;
> +    }
> +
> +    hw_error("EDU: DMA range 0x%.8x-0x%.8x out of bounds (0x%.8x-0x%.8x)!",
> +            addr, end1 - 1, start, end2 - 1);
> +}
> +
> +static dma_addr_t edu_clamp_addr(const EduState *edu, dma_addr_t addr)
> +{
> +    dma_addr_t res = addr & edu->dma_mask;
> +
> +    if (addr != res) {
> +        printf("EDU: clamping DMA 0x%.16lx to 0x%.16lx!\n", addr, res);
> +    }
> +
> +    return res;
> +}
> +
> +static void edu_dma_timer(void *opaque)
> +{
> +    EduState *edu = opaque;
> +    bool raise_irq = false;
> +
> +    if (!(edu->dma.cmd & EDU_DMA_RUN)) {
> +        return;
> +    }
> +
> +    if (EDU_DMA_DIR(edu->dma.cmd) == EDU_DMA_FROM_PCI) {
> +        uint32_t dst = edu->dma.dst;
> +        edu_check_range(dst, edu->dma.cnt, DMA_START, DMA_SIZE);
> +        dst -= DMA_START;
> +        pci_dma_read(&edu->pdev, edu_clamp_addr(edu, edu->dma.src),
> +                edu->dma_buf + dst, edu->dma.cnt);
> +    } else {
> +        uint32_t src = edu->dma.src;
> +        edu_check_range(src, edu->dma.cnt, DMA_START, DMA_SIZE);
> +        src -= DMA_START;
> +        pci_dma_write(&edu->pdev, edu_clamp_addr(edu, edu->dma.dst),
> +                edu->dma_buf + src, edu->dma.cnt);
> +    }
> +
> +    edu->dma.cmd &= ~EDU_DMA_RUN;
> +    if (edu->dma.cmd & EDU_DMA_IRQ) {
> +        raise_irq = true;
> +    }
> +
> +    if (raise_irq) {
> +        edu_raise_irq(edu, DMA_IRQ);
> +    }
> +}
> +
> +static void dma_rw(EduState *edu, bool write, dma_addr_t *val, dma_addr_t *dma,
> +                bool timer)
> +{
> +    if (write && (edu->dma.cmd & EDU_DMA_RUN)) {
> +        return;
> +    }
> +
> +    if (write) {
> +        *dma = *val;
> +    } else {
> +        *val = *dma;
> +    }
> +
> +    if (timer) {
> +        timer_mod(&edu->dma_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 100);
> +    }
> +}
> +
> +static uint64_t edu_mmio_read(void *opaque, hwaddr addr, unsigned size)
> +{
> +    EduState *edu = opaque;
> +    uint64_t val = ~0ULL;
> +
> +    if (size != 4) {
> +        return val;
> +    }
> +
> +    switch (addr) {
> +    case 0x00:
> +        val = 0x010000edu;
> +        break;
> +    case 0x04:
> +        val = edu->addr4;
> +        break;
> +    case 0x08:
> +        qemu_mutex_lock(&edu->thr_mutex);
> +        val = edu->fact;
> +        qemu_mutex_unlock(&edu->thr_mutex);
> +        break;
> +    case 0x20:
> +        val = atomic_read(&edu->status);
> +        break;
> +    case 0x24:
> +        val = edu->irq_status;
> +        break;
> +    case 0x80:
> +        dma_rw(edu, false, &val, &edu->dma.src, false);
> +        break;
> +    case 0x88:
> +        dma_rw(edu, false, &val, &edu->dma.dst, false);
> +        break;
> +    case 0x90:
> +        dma_rw(edu, false, &val, &edu->dma.cnt, false);
> +        break;
> +    case 0x98:
> +        dma_rw(edu, false, &val, &edu->dma.cmd, false);
> +        break;
> +    }
> +
> +    return val;
> +}
> +
> +static void edu_mmio_write(void *opaque, hwaddr addr, uint64_t val,
> +                unsigned size)
> +{
> +    EduState *edu = opaque;
> +
> +    if (addr < 0x80 && size != 4) {
> +        return;
> +    }
> +
> +    if (addr >= 0x80 && size != 4 && size != 8) {
> +        return;
> +    }
> +
> +    switch (addr) {
> +    case 0x04:
> +        edu->addr4 = ~val;
> +        break;
> +    case 0x08:
> +        if (atomic_read(&edu->status) & EDU_STATUS_COMPUTING) {
> +            break;
> +        }
> +        /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because it is only
> +         * set in this function and it is under the iothread mutex.
> +         */
> +        qemu_mutex_lock(&edu->thr_mutex);
> +        edu->fact = val;
> +        atomic_or(&edu->status, EDU_STATUS_COMPUTING);
> +        qemu_cond_signal(&edu->thr_cond);
> +        qemu_mutex_unlock(&edu->thr_mutex);
> +        break;
> +    case 0x20:
> +        if (val & EDU_STATUS_IRQFACT) {
> +            atomic_or(&edu->status, EDU_STATUS_IRQFACT);
> +        } else {
> +            atomic_and(&edu->status, ~EDU_STATUS_IRQFACT);
> +        }
> +        break;
> +    case 0x60:
> +        edu_raise_irq(edu, val);
> +        break;
> +    case 0x64:
> +        edu_lower_irq(edu, val);
> +        break;
> +    case 0x80:
> +        dma_rw(edu, true, &val, &edu->dma.src, false);
> +        break;
> +    case 0x88:
> +        dma_rw(edu, true, &val, &edu->dma.dst, false);
> +        break;
> +    case 0x90:
> +        dma_rw(edu, true, &val, &edu->dma.cnt, false);
> +        break;
> +    case 0x98:
> +        if (!(val & EDU_DMA_RUN)) {
> +            break;
> +        }
> +        dma_rw(edu, true, &val, &edu->dma.cmd, true);
> +        break;
> +    }
> +}
> +
> +static const MemoryRegionOps edu_mmio_ops = {
> +    .read = edu_mmio_read,
> +    .write = edu_mmio_write,
> +    .endianness = DEVICE_NATIVE_ENDIAN,
> +};
> +
> +/*
> + * We purposedly use a thread, so that users are forced to wait for the status
> + * register.
> + */
> +static void *edu_fact_thread(void *opaque)
> +{
> +    EduState *edu = opaque;
> +
> +    while (1) {
> +        uint32_t val, ret = 1;
> +
> +        qemu_mutex_lock(&edu->thr_mutex);
> +        while ((atomic_read(&edu->status) & EDU_STATUS_COMPUTING) == 0 &&
> +                        !edu->stopping) {
> +            qemu_cond_wait(&edu->thr_cond, &edu->thr_mutex);
> +        }
> +
> +        if (edu->stopping) {
> +            qemu_mutex_unlock(&edu->thr_mutex);
> +            break;
> +        }
> +
> +        val = edu->fact;
> +        qemu_mutex_unlock(&edu->thr_mutex);
> +
> +        while (val > 0) {
> +            ret *= val--;
> +        }
> +
> +        /*
> +         * We should sleep for a random period here, so that students are
> +         * forced to check the status properly.
> +         */
> +
> +        qemu_mutex_lock(&edu->thr_mutex);
> +        edu->fact = ret;
> +        qemu_mutex_unlock(&edu->thr_mutex);
> +        atomic_and(&edu->status, ~EDU_STATUS_COMPUTING);
> +
> +        if (atomic_read(&edu->status) & EDU_STATUS_IRQFACT) {
> +            qemu_mutex_lock_iothread();
> +            edu_raise_irq(edu, FACT_IRQ);
> +            qemu_mutex_unlock_iothread();
> +        }
> +    }
> +
> +    return NULL;
> +}
> +
> +static int pci_edu_init(PCIDevice *pdev)
> +{
> +    EduState *edu = DO_UPCAST(EduState, pdev, pdev);
> +    uint8_t *pci_conf = pdev->config;
> +
> +    timer_init(&edu->dma_timer, main_loop_tlg.tl[QEMU_CLOCK_VIRTUAL], SCALE_MS,
> +            edu_dma_timer, edu);
> +
> +    qemu_mutex_init(&edu->thr_mutex);
> +    qemu_cond_init(&edu->thr_cond);
> +    qemu_thread_create(&edu->thread, "edu", edu_fact_thread,
> +                       edu, QEMU_THREAD_JOINABLE);
> +
> +    pci_config_set_interrupt_pin(pci_conf, 1);
> +
> +    memory_region_init_io(&edu->mmio, OBJECT(edu), &edu_mmio_ops, edu,
> +                    "edu-mmio", 1 << 20);
> +    pci_register_bar(pdev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &edu->mmio);
> +
> +    return 0;
> +}
> +
> +static void pci_edu_uninit(PCIDevice *pdev)
> +{
> +    EduState *edu = DO_UPCAST(EduState, pdev, pdev);
> +
> +    qemu_mutex_lock(&edu->thr_mutex);
> +    edu->stopping = true;
> +    qemu_mutex_unlock(&edu->thr_mutex);
> +    qemu_cond_signal(&edu->thr_cond);
> +    qemu_thread_join(&edu->thread);
> +
> +    qemu_cond_destroy(&edu->thr_cond);
> +    qemu_mutex_destroy(&edu->thr_mutex);
> +
> +    timer_del(&edu->dma_timer);
> +}
> +
> +static void edu_obj_uint64(Object *obj, struct Visitor *v, void *opaque,
> +                const char *name, Error **errp)
> +{
> +    uint64_t *val = opaque;
> +
> +    visit_type_uint64(v, val, name, errp);
> +}
> +
> +static void edu_instance_init(Object *obj)
> +{
> +    EduState *edu = EDU(obj);
> +
> +    edu->dma_mask = (1UL << 28) - 1;
> +    object_property_add(obj, "dma_mask", "uint64", edu_obj_uint64,
> +                    edu_obj_uint64, NULL, &edu->dma_mask, NULL);
> +}
> +
> +static void edu_class_init(ObjectClass *class, void *data)
> +{
> +    PCIDeviceClass *k = PCI_DEVICE_CLASS(class);
> +
> +    k->init = pci_edu_init;
> +    k->exit = pci_edu_uninit;
> +    k->vendor_id = PCI_VENDOR_ID_QEMU;
> +    k->device_id = 0x11e8;
> +    k->revision = 0x10;
> +    k->class_id = PCI_CLASS_OTHERS;
> +}
> +
> +static void pci_edu_register_types(void)
> +{
> +    static const TypeInfo edu_info = {
> +        .name          = "edu",
> +        .parent        = TYPE_PCI_DEVICE,
> +        .instance_size = sizeof(EduState),
> +        .instance_init = edu_instance_init,
> +        .class_init    = edu_class_init,
> +    };
> +
> +    type_register_static(&edu_info);
> +}
> +type_init(pci_edu_register_types)
> 

Applied, thanks.  Pull request should come later this week or early next
week.

Paolo

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH v5 1/1] hw: misc, add educational driver
  2015-01-21 16:48   ` [Qemu-devel] [PATCH v5 1/1] hw: misc, " Jiri Slaby
  2015-01-21 16:58     ` Paolo Bonzini
@ 2015-01-21 23:37     ` Eric Blake
  2015-01-22  8:22       ` [Qemu-devel] [PATCH 1/1] edu: fix license information Jiri Slaby
  2015-01-21 23:49     ` [Qemu-devel] [PATCH v5 1/1] hw: misc, add educational driver Eric Blake
  2 siblings, 1 reply; 15+ messages in thread
From: Eric Blake @ 2015-01-21 23:37 UTC (permalink / raw)
  To: Jiri Slaby, qemu-devel; +Cc: Paolo Bonzini

[-- Attachment #1: Type: text/plain, Size: 1136 bytes --]

On 01/21/2015 09:48 AM, Jiri Slaby wrote:
> I am using qemu for teaching the Linux kernel at our university. I
> wrote a simple PCI device that can answer to writes/reads, generate
> interrupts and perform DMA. As I am dragging it locally over 2 years,
> I am sending it to you now.
> 
> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
> ---

> +++ b/docs/specs/edu.txt
> @@ -0,0 +1,106 @@
> +
> +EDU device
> +==========
> +
> +This is an educational device for writing (kernel) drivers. Its original
> +intention was to support the Linux kernel lectures taught at the Masaryk
> +University. Students are given this virtual device and are expected to write a
> +driver with I/Os, IRQs, DMAs and such.

Just because we have lots of bad examples in this directory is not an
excuse - I've been requesting that new doc files explicitly call out
copyright and license information, rather than implicitly relying on the
defaults of GPLv2+ from the top level.  It would be fine to add that as
a followup patch.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH v5 1/1] hw: misc, add educational driver
  2015-01-21 16:48   ` [Qemu-devel] [PATCH v5 1/1] hw: misc, " Jiri Slaby
  2015-01-21 16:58     ` Paolo Bonzini
  2015-01-21 23:37     ` Eric Blake
@ 2015-01-21 23:49     ` Eric Blake
  2 siblings, 0 replies; 15+ messages in thread
From: Eric Blake @ 2015-01-21 23:49 UTC (permalink / raw)
  To: Jiri Slaby, qemu-devel; +Cc: Paolo Bonzini

[-- Attachment #1: Type: text/plain, Size: 1141 bytes --]

On 01/21/2015 09:48 AM, Jiri Slaby wrote:
> I am using qemu for teaching the Linux kernel at our university. I
> wrote a simple PCI device that can answer to writes/reads, generate
> interrupts and perform DMA. As I am dragging it locally over 2 years,
> I am sending it to you now.
> 
> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
> ---

> +++ b/docs/specs/edu.txt
> @@ -0,0 +1,106 @@
> +
> +EDU device
> +==========
> +
> +This is an educational device for writing (kernel) drivers. Its original
> +intention was to support the Linux kernel lectures taught at the Masaryk
> +University. Students are given this virtual device and are expected to write a
> +driver with I/Os, IRQs, DMAs and such.

Just because many other doc files in this directory are bad examples is
not a good excuse - I've been requesting that new docs include an
explicit license and copyright blurb, instead of relying on implicit
defaults of GPLv2+ inherited from the top level.  It would be fine to
add that as a followup patch.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [Qemu-devel] [PATCH 1/1] edu: fix license information
  2015-01-21 23:37     ` Eric Blake
@ 2015-01-22  8:22       ` Jiri Slaby
  2015-01-22  9:04         ` Markus Armbruster
  2015-01-22 15:39         ` Eric Blake
  0 siblings, 2 replies; 15+ messages in thread
From: Jiri Slaby @ 2015-01-22  8:22 UTC (permalink / raw)
  To: qemu-devel; +Cc: Paolo Bonzini, Jiri Slaby

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
---
 docs/specs/edu.txt | 4 ++++
 hw/misc/edu.c      | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt
index 360af27ec8b1..1a23df9d21f6 100644
--- a/docs/specs/edu.txt
+++ b/docs/specs/edu.txt
@@ -2,6 +2,10 @@
 EDU device
 ==========
 
+Copyleft (c) 2014-2015 Jiri Slaby
+
+This document is licensed under the GPLv2 (or later).
+
 This is an educational device for writing (kernel) drivers. Its original
 intention was to support the Linux kernel lectures taught at the Masaryk
 University. Students are given this virtual device and are expected to write a
diff --git a/hw/misc/edu.c b/hw/misc/edu.c
index c74f9b64540d..9a8088c6898d 100644
--- a/hw/misc/edu.c
+++ b/hw/misc/edu.c
@@ -1,7 +1,7 @@
 /*
  * QEMU educational PCI device
  *
- * Copyright (c) 2012-2014 Jiri Slaby
+ * Copyright (c) 2012-2015 Jiri Slaby
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
-- 
2.2.1

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH 1/1] edu: fix license information
  2015-01-22  8:22       ` [Qemu-devel] [PATCH 1/1] edu: fix license information Jiri Slaby
@ 2015-01-22  9:04         ` Markus Armbruster
  2015-01-22  9:04           ` Jiri Slaby
  2015-01-22 15:39         ` Eric Blake
  1 sibling, 1 reply; 15+ messages in thread
From: Markus Armbruster @ 2015-01-22  9:04 UTC (permalink / raw)
  To: Jiri Slaby; +Cc: Paolo Bonzini, qemu-devel

Jiri Slaby <jslaby@suse.cz> writes:

> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
> ---
>  docs/specs/edu.txt | 4 ++++
>  hw/misc/edu.c      | 2 +-
>  2 files changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt
> index 360af27ec8b1..1a23df9d21f6 100644
> --- a/docs/specs/edu.txt
> +++ b/docs/specs/edu.txt
> @@ -2,6 +2,10 @@
>  EDU device
>  ==========
>  
> +Copyleft (c) 2014-2015 Jiri Slaby

IANAL, but I'm afraid you have to spell this "Copyright" to carry legal
weight.

> +
> +This document is licensed under the GPLv2 (or later).
> +
>  This is an educational device for writing (kernel) drivers. Its original
>  intention was to support the Linux kernel lectures taught at the Masaryk
>  University. Students are given this virtual device and are expected to write a
> diff --git a/hw/misc/edu.c b/hw/misc/edu.c
> index c74f9b64540d..9a8088c6898d 100644
> --- a/hw/misc/edu.c
> +++ b/hw/misc/edu.c
> @@ -1,7 +1,7 @@
>  /*
>   * QEMU educational PCI device
>   *
> - * Copyright (c) 2012-2014 Jiri Slaby
> + * Copyright (c) 2012-2015 Jiri Slaby
>   *
>   * Permission is hereby granted, free of charge, to any person obtaining a
>   * copy of this software and associated documentation files (the "Software"),

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH 1/1] edu: fix license information
  2015-01-22  9:04         ` Markus Armbruster
@ 2015-01-22  9:04           ` Jiri Slaby
  2015-01-22  9:41             ` Markus Armbruster
  0 siblings, 1 reply; 15+ messages in thread
From: Jiri Slaby @ 2015-01-22  9:04 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: Paolo Bonzini, qemu-devel

On 01/22/2015, 10:04 AM, Markus Armbruster wrote:
> Jiri Slaby <jslaby@suse.cz> writes:
> 
>> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
>> ---
>>  docs/specs/edu.txt | 4 ++++
>>  hw/misc/edu.c      | 2 +-
>>  2 files changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt
>> index 360af27ec8b1..1a23df9d21f6 100644
>> --- a/docs/specs/edu.txt
>> +++ b/docs/specs/edu.txt
>> @@ -2,6 +2,10 @@
>>  EDU device
>>  ==========
>>  
>> +Copyleft (c) 2014-2015 Jiri Slaby
> 
> IANAL, but I'm afraid you have to spell this "Copyright" to carry legal
> weight.

Not quite as (c) is enough. GPLv2 is a copyleft license.

-- 
js
suse labs

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH 1/1] edu: fix license information
  2015-01-22  9:04           ` Jiri Slaby
@ 2015-01-22  9:41             ` Markus Armbruster
  0 siblings, 0 replies; 15+ messages in thread
From: Markus Armbruster @ 2015-01-22  9:41 UTC (permalink / raw)
  To: Jiri Slaby; +Cc: Paolo Bonzini, qemu-devel

Jiri Slaby <jslaby@suse.cz> writes:

> On 01/22/2015, 10:04 AM, Markus Armbruster wrote:
>> Jiri Slaby <jslaby@suse.cz> writes:
>> 
>>> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
>>> ---
>>>  docs/specs/edu.txt | 4 ++++
>>>  hw/misc/edu.c      | 2 +-
>>>  2 files changed, 5 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt
>>> index 360af27ec8b1..1a23df9d21f6 100644
>>> --- a/docs/specs/edu.txt
>>> +++ b/docs/specs/edu.txt
>>> @@ -2,6 +2,10 @@
>>>  EDU device
>>>  ==========
>>>  
>>> +Copyleft (c) 2014-2015 Jiri Slaby
>> 
>> IANAL, but I'm afraid you have to spell this "Copyright" to carry legal
>> weight.
>
> Not quite as (c) is enough. GPLv2 is a copyleft license.

Quoting <https://www.gnu.org/licenses/gpl-howto.en.html>:

    Always use the English word “Copyright”; by international
    convention, this is used worldwide, even for material in other
    languages. The copyright symbol “©” can be included if you wish (and
    your character set supports it), but it's not necessary. There is no
    legal significance to using the three-character sequence “(C)”,
    although it does no harm.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH 1/1] edu: fix license information
  2015-01-22  8:22       ` [Qemu-devel] [PATCH 1/1] edu: fix license information Jiri Slaby
  2015-01-22  9:04         ` Markus Armbruster
@ 2015-01-22 15:39         ` Eric Blake
  2015-01-22 15:53           ` Paolo Bonzini
  1 sibling, 1 reply; 15+ messages in thread
From: Eric Blake @ 2015-01-22 15:39 UTC (permalink / raw)
  To: Jiri Slaby, qemu-devel; +Cc: Paolo Bonzini

[-- Attachment #1: Type: text/plain, Size: 704 bytes --]

On 01/22/2015 01:22 AM, Jiri Slaby wrote:
> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
> ---
>  docs/specs/edu.txt | 4 ++++
>  hw/misc/edu.c      | 2 +-
>  2 files changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt
> index 360af27ec8b1..1a23df9d21f6 100644
> --- a/docs/specs/edu.txt
> +++ b/docs/specs/edu.txt
> @@ -2,6 +2,10 @@
>  EDU device
>  ==========
>  
> +Copyleft (c) 2014-2015 Jiri Slaby

s/Copyleft/Copyright/.  There's legal implications to the word you use,
and the rest of the code base does not use "Copyleft".

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH 1/1] edu: fix license information
  2015-01-22 15:39         ` Eric Blake
@ 2015-01-22 15:53           ` Paolo Bonzini
  2015-01-22 19:10             ` Jiri Slaby
  0 siblings, 1 reply; 15+ messages in thread
From: Paolo Bonzini @ 2015-01-22 15:53 UTC (permalink / raw)
  To: Eric Blake, Jiri Slaby, qemu-devel



On 22/01/2015 16:39, Eric Blake wrote:
> On 01/22/2015 01:22 AM, Jiri Slaby wrote:
>> Signed-off-by: Jiri Slaby <jslaby@suse.cz> --- docs/specs/edu.txt
>> | 4 ++++ hw/misc/edu.c      | 2 +- 2 files changed, 5
>> insertions(+), 1 deletion(-)
>> 
>> diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt index
>> 360af27ec8b1..1a23df9d21f6 100644 --- a/docs/specs/edu.txt +++
>> b/docs/specs/edu.txt @@ -2,6 +2,10 @@ EDU device ==========
>> 
>> +Copyleft (c) 2014-2015 Jiri Slaby
> 
> s/Copyleft/Copyright/.  There's legal implications to the word you
> use, and the rest of the code base does not use "Copyleft".
> 

Yup, fixed and applied.

Paolo

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH 1/1] edu: fix license information
  2015-01-22 15:53           ` Paolo Bonzini
@ 2015-01-22 19:10             ` Jiri Slaby
  2015-01-23  7:40               ` Markus Armbruster
  0 siblings, 1 reply; 15+ messages in thread
From: Jiri Slaby @ 2015-01-22 19:10 UTC (permalink / raw)
  To: Paolo Bonzini, Eric Blake, qemu-devel

On 01/22/2015, 04:53 PM, Paolo Bonzini wrote:
> On 22/01/2015 16:39, Eric Blake wrote:
>> On 01/22/2015 01:22 AM, Jiri Slaby wrote:
>>> Signed-off-by: Jiri Slaby <jslaby@suse.cz> --- docs/specs/edu.txt
>>> | 4 ++++ hw/misc/edu.c      | 2 +- 2 files changed, 5
>>> insertions(+), 1 deletion(-)
>>>
>>> diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt index
>>> 360af27ec8b1..1a23df9d21f6 100644 --- a/docs/specs/edu.txt +++
>>> b/docs/specs/edu.txt @@ -2,6 +2,10 @@ EDU device ==========
>>>
>>> +Copyleft (c) 2014-2015 Jiri Slaby
>>
>> s/Copyleft/Copyright/.  There's legal implications to the word you
>> use, and the rest of the code base does not use "Copyleft".
>>
> 
> Yup, fixed and applied.

Ok, thanks a lot! (I still think "copyleft (c)" was enough (in all laws
we care) ;).)

-- 
js
suse labs

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Qemu-devel] [PATCH 1/1] edu: fix license information
  2015-01-22 19:10             ` Jiri Slaby
@ 2015-01-23  7:40               ` Markus Armbruster
  0 siblings, 0 replies; 15+ messages in thread
From: Markus Armbruster @ 2015-01-23  7:40 UTC (permalink / raw)
  To: Jiri Slaby; +Cc: Paolo Bonzini, qemu-devel

Jiri Slaby <jslaby@suse.cz> writes:

> On 01/22/2015, 04:53 PM, Paolo Bonzini wrote:
>> On 22/01/2015 16:39, Eric Blake wrote:
>>> On 01/22/2015 01:22 AM, Jiri Slaby wrote:
>>>> Signed-off-by: Jiri Slaby <jslaby@suse.cz> --- docs/specs/edu.txt
>>>> | 4 ++++ hw/misc/edu.c      | 2 +- 2 files changed, 5
>>>> insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/docs/specs/edu.txt b/docs/specs/edu.txt index
>>>> 360af27ec8b1..1a23df9d21f6 100644 --- a/docs/specs/edu.txt +++
>>>> b/docs/specs/edu.txt @@ -2,6 +2,10 @@ EDU device ==========
>>>>
>>>> +Copyleft (c) 2014-2015 Jiri Slaby
>>>
>>> s/Copyleft/Copyright/.  There's legal implications to the word you
>>> use, and the rest of the code base does not use "Copyleft".
>>>
>> 
>> Yup, fixed and applied.
>
> Ok, thanks a lot! (I still think "copyleft (c)" was enough (in all laws
> we care) ;).)

Do not to deviate from the FSF's advice on how their licenses should be
applied unless

* you're a lawyer in all the jurisdictions we care about, or

* you got legal advice in all the jurisdictions we care about, or

* you're eagerly looking forward to needing legal advice in the future.

Since none of the above apply to the QEMU project, we stick to the FSF's
advice.

There are ample opportunities for self-expression in source code and
documentation other than legal notices.

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2015-01-23  7:40 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-21 16:28 [Qemu-devel] [PATCH v4 1/1] pci-host: add educational driver Jiri Slaby
2015-01-21 16:40 ` Paolo Bonzini
2015-01-21 16:44   ` Jiri Slaby
2015-01-21 16:48   ` [Qemu-devel] [PATCH v5 1/1] hw: misc, " Jiri Slaby
2015-01-21 16:58     ` Paolo Bonzini
2015-01-21 23:37     ` Eric Blake
2015-01-22  8:22       ` [Qemu-devel] [PATCH 1/1] edu: fix license information Jiri Slaby
2015-01-22  9:04         ` Markus Armbruster
2015-01-22  9:04           ` Jiri Slaby
2015-01-22  9:41             ` Markus Armbruster
2015-01-22 15:39         ` Eric Blake
2015-01-22 15:53           ` Paolo Bonzini
2015-01-22 19:10             ` Jiri Slaby
2015-01-23  7:40               ` Markus Armbruster
2015-01-21 23:49     ` [Qemu-devel] [PATCH v5 1/1] hw: misc, add educational driver Eric Blake

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.