hw/pci/pci_example.c : Added a new pci device
RFC 1/1 hw/pci/pci_example.c : Added a new pci device
- this is a simple example of how to write a pci device that supports portio, mmio, irq and dma
--- hw/pci/Makefile.objs | 1 + hw/pci/pci_example.c | 309 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+) create mode 100644 hw/pci/pci_example.c
hw/pci/Makefile.objs +1 -0
@@ -4,6 +4,7 @@ common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pci_example.o common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.o
hw/pci/pci_example.c new file +309 -0
309 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 2 lines · Yoni Bettan
- this is a simple example of how to write a pci device that supports portio, mmio, irq and dma
As the commit message miss a little bit of information I will add it here and update the commit message in the next Version.
The main goal is to add 2 example devices to be used as a template or a guideline for contributors when they wish to create a new device.
Those devices may be compiled by default with qemu, to ensure they won't break, but not have to be linked, so performance won't decrease.
Another reason for such devices is to document "the right way" to write a new device in qemu.
* PCI device:
o its driver is located at
https://github.com/ybettan/QemuDeviceDrivers.git
* virtIO device:
o its driver isn't written yet but will be added to the same Github repository written above.
o when this device will be done the next step is to insert it into the virtio specification.
Both devices can be found at https://github.com/ybettan/qemu.git under 'pci' and 'virtio' branches.
In addition I am writing a blog to give a logical overview of the virtio protocol and a step-by-step guide to write a new virtio device.
This blog can be found at https://howtovms.wordpress.com. <https://howtovms.wordpress.com>
Quoted 335 lines
Signed-off-by Yoni Bettan <[email protected]>--- hw/pci/Makefile.objs | 1 + hw/pci/pci_example.c | 309 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+)create mode 100644
hw/pci/pci_example.chw/pci/Makefile.objs +1
-0@@ -4,6 +4,7 @@ common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pci_example.o common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci_example.c new file +309
-0@@ -0,0 +1,309 @@ +#include "qemu/osdep.h" +#include "hw/pci/pci.h" + +#define TYPE_PCI_EXAMPLE "pci-example" + +#define PCI_EXAMPLE (obj) \ + OBJECT_CHECK(PCIExampleDevState, (obj), TYPE_PCI_EXAMPLE) + + +#define ERROR -1 +#define BLOCK_SIZE 64 +#define EXAMPLE_MMIO_SIZE BLOCK_SIZE +#define EXAMPLE_PIO_SIZE BLOCK_SIZE +#define DMA_BUFF_SIZE 4096 + +/*---------------------------------------------------------------------------*/ +/* PCI Struct */ +/*---------------------------------------------------------------------------*/ + +typedef struct PCIExampleDevState { +/*< private >*/ +PCIDevice parent_obj; +/*< public >*/ + +/* memory region */ +MemoryRegion portio; +MemoryRegion mmio; +MemoryRegion irqio; +MemoryRegion dmaio; + +/* data registers */ +uint64_t memData, ioData, dmaPhisicalBase; + +qemu_irq irq; +/* for the driver to determine if this device caused the interrupt */ +uint64_t threwIrq; + +}PCIExampleDevState ; + + +/*---------------------------------------------------------------------------*/ +/* Read/Write functions */ +/*---------------------------------------------------------------------------*/ + +/* do nothing because the mmio read is done from DMA buffer */ +static uint64_t pci_example_mmio_read (void *opaque,hwaddr addr,unsigned size) +{ +return ERROR ; +} + +static void +pci_example_mmio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; +PCIDevice *pciDev = (PCIDevice *)opaque; + +if (size !=1 ) { +return ; + } + +/* compute the result */ + pms->memData = val *2 ; + +/* write the result directly to phisical memory */ +cpu_physical_memory_write (pms->dmaPhisicalBase, &pms->memData, +DMA_BUFF_SIZE ); + +/* raise an IRQ to notify DMA has finished */ + pms->threwIrq =1 ; +pci_irq_assert (pciDev); +} + +static uint64_t pci_example_pio_read (void *opaque,hwaddr addr,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; + +if (size !=1 ) { +return ERROR ; + } + +return pms->ioData; +} + +static void +pci_example_pio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; +PCIDevice *pciDev = (PCIDevice *)opaque; + +if (size !=1 ) { +return ; + } + + pms->ioData = val *2 ; + pms->threwIrq =1 ; +pci_irq_assert (pciDev); +} + +static uint64_t pci_example_irqio_read (void *opaque,hwaddr addr,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; + +if (size !=1 ) { +return ERROR ; + } + +return pms->threwIrq; +} + +static void +pci_example_irqio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; +PCIDevice *pciDev = (PCIDevice *)opaque; + +if (size !=1 ) { +return ; + } + +/* give the ability to assert IRQ , we will use it only to deassert IRQ */ +if (val) { +pci_irq_assert (pciDev); + pms->threwIrq =1 ; + }else { + pms->threwIrq =0 ; +pci_irq_deassert (pciDev); + } +} + +/* do nothing because physical DMA buffer addres is onlyt set and don't need to +* be red */ +static uint64_t +pci_example_dma_base_read (void *opaque,hwaddr addr,unsigned size) +{ +return ERROR ; +} + +static void +pci_example_dma_base_write (void *opaque,hwaddr addr,uint64_t val, +unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; + +if (size !=4 ) { +return ; + } + +/* notify the device about the physical address of the DMA buffer that the +* driver has allocated */ +switch (addr) { +/* lower bytes */ +case (0 ): + pms->dmaPhisicalBase &=0xffffffff00000000 ; +break ; + +/* upper bytes */ +case (4 ): + val <<=32 ; + pms->dmaPhisicalBase &=0x00000000ffffffff ; +break ; + } + + pms->dmaPhisicalBase |= val; +} + +/*---------------------------------------------------------------------------*/ +/* PCI region ops */ +/*---------------------------------------------------------------------------*/ + +/* callback called when memory region representing the MMIO space is accessed */ +static const MemoryRegionOps pci_example_mmio_ops = { + .read = pci_example_mmio_read, + .write = pci_example_mmio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 , + }, +}; + +/* callback called when memory region representing the PIO space is accessed */ +static const MemoryRegionOps pci_example_pio_ops = { + .read = pci_example_pio_read, + .write = pci_example_pio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 , + }, +}; + +/* callback called when memory region representing the IRQ space is accessed */ +static const MemoryRegionOps pci_example_irqio_ops = { + .read = pci_example_irqio_read, + .write = pci_example_irqio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 , + }, +}; + +/* callback called when memory region representing the DMA space is accessed */ +static const MemoryRegionOps pci_example_dma_ops = { + .read = pci_example_dma_base_read, + .write = pci_example_dma_base_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =4 , + .max_access_size =4 , + }, +}; + +/*---------------------------------------------------------------------------*/ +/* PCI functions */ +/*---------------------------------------------------------------------------*/ + +/* this is called when lunching the vm with "-device <device name>" */ +static void pci_example_realize (PCIDevice *pciDev,Error **errp) +{ +PCIExampleDevState *d =PCI_EXAMPLE (pciDev); +uint8_t *pciCond = pciDev->config; + + d->threwIrq =0 ; + +/* initiallise the memory region of the CPU to the device */ +memory_region_init_io (&d->mmio,OBJECT (d), &pci_example_mmio_ops, d, +"pci-example-mmio" ,EXAMPLE_MMIO_SIZE ); + +memory_region_init_io (&d->portio,OBJECT (d), &pci_example_pio_ops, d, +"pci-example-portio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->irqio,OBJECT (d), &pci_example_irqio_ops, d, +"pci-example-irqio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->dmaio,OBJECT (d), &pci_example_dma_ops, d, +"pci-example-dma-base" ,EXAMPLE_MMIO_SIZE ); + +/* alocate BARs */ +pci_register_bar (pciDev,0 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->mmio); +pci_register_bar (pciDev,1 ,PCI_BASE_ADDRESS_SPACE_IO , &d->portio); +pci_register_bar (pciDev,2 ,PCI_BASE_ADDRESS_SPACE_IO , &d->irqio); +pci_register_bar (pciDev,3 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->dmaio); + +/* give interrupt support. +* a pci device has 4 pin for interrupt, here we use pin A */ +pci_config_set_interrupt_pin (pciCond,1 ); +} + + +/* the destructor of pci_example_realize() */ +static void pci_example_uninit (PCIDevice *dev) +{ +/* unregister BARs and other stuff */ +} + + +/* class constructor */ +static void pci_example_class_init (ObjectClass *klass,void *data) +{ +/* sort of dynamic cast */ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *k =PCI_DEVICE_CLASS (klass); + + k->realize = pci_example_realize; + k->exit = pci_example_uninit; + +/* some regular IDs in HEXA */ + k->vendor_id =PCI_VENDOR_ID_REDHAT ; + k->device_id =PCI_DEVICE_ID_REDHAT_TEST ; + k->class_id =PCI_CLASS_OTHERS ; + +/* set the device bitmap category */ +set_bit (DEVICE_CATEGORY_MISC , dc->categories); + + k->revision =0x00 ; + dc->desc ="PCI Example Device" ; +} + +/*---------------------------------------------------------------------------*/ +/* QEMU overhead */ +/*---------------------------------------------------------------------------*/ + + +/* Contains all the informations of the device we are creating. +* class_init will be called when we are defining our device. */ +static const TypeInfo pci_example_info = { + .name =TYPE_PCI_EXAMPLE , + .parent =TYPE_PCI_DEVICE , + .instance_size =sizeof (PCIExampleDevState), + .class_init = pci_example_class_init, + .interfaces = (InterfaceInfo []) { + {INTERFACE_CONVENTIONAL_PCI_DEVICE }, + { }, + }, +}; + + +/* function called before the qemu main it will define our device */ +static void pci_example_register_types (void ) +{ +type_register_static (&pci_example_info); +} + +/* make qemu register our device at qemu-booting */ +type_init (pci_example_register_types) + + +
Quoted 2 lines · On Tue, 28 Aug 2018 10:24:26 +0300 Yoni Bettan <[email protected]> wrote:
- this is a simple example of how to write a pci device that supports portio, mmio, irq and dma
Do you also plan to add example code for MSI(-X)?
[Not just asking because s390 only supports MSI-X. Honestly :)]
Quoted 8 lines
Signed-off-by Yoni Bettan <[email protected]>--- hw/pci/Makefile.objs | 1 + hw/pci/pci_example.c | 309 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+) create mode 100644 hw/pci/pci_example.c
(...)
Quoted 6 lines
hw/pci/pci_example.c new file +0
-0
hw/pci/pci_example.c line 10
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0@@ -0,0 +1,309 @@ +#include "qemu/osdep.h" +#include "hw/pci/pci.h" + +#define TYPE_PCI_EXAMPLE "pci-example" + +#define PCI_EXAMPLE (obj) \ + OBJECT_CHECK(PCIExampleDevState, (obj), TYPE_PCI_EXAMPLE) + + +#define ERROR -1
It's probably not a good idea to obfuscate the return code. Either return -1, or a proper error code.
Quoted 12 lines
hw/pci/pci_example.c new file +12
-0+#define BLOCK_SIZE 64 +#define EXAMPLE_MMIO_SIZE BLOCK_SIZE +#define EXAMPLE_PIO_SIZE BLOCK_SIZE +#define DMA_BUFF_SIZE 4096 + +/*---------------------------------------------------------------------------*/ +/* PCI Struct */ +/*---------------------------------------------------------------------------*/ + +typedef struct PCIExampleDevState { + /*< private >*/ + PCIDevice parent_obj;
hw/pci/pci_example.c line 32
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+/*< public >*/ + +/* memory region */ +MemoryRegion portio; +MemoryRegion mmio; +MemoryRegion irqio; +MemoryRegion dmaio; + +/* data registers */ +uint64_t memData, ioData, dmaPhisicalBase;
I think we want this to be mem_data, io_data, and dma_physical_base instead.
hw/pci/pci_example.c line 36
Quoted 4 lines
hw/pci/pci_example.c new file +4
-0+ +qemu_irq irq; +/* for the driver to determine if this device caused the interrupt */ +uint64_t threwIrq;
Same here (threw_irq).
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+ +} PCIExampleDevState; + + +/*---------------------------------------------------------------------------*/ +/* Read/Write functions */
hw/pci/pci_example.c line 52
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+/*---------------------------------------------------------------------------*/ + +/* do nothing because the mmio read is done from DMA buffer */ +static uint64_t pci_example_mmio_read (void *opaque,hwaddr addr,unsigned size) +{ +return ERROR ; +} + +static void +pci_example_mmio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size)
I think our preferred style is to keep return value and function name on the same line and add line breaks in the parameter list, if needed. (Some more occurrences further down.)
hw/pci/pci_example.c line 54
Quoted 2 lines
hw/pci/pci_example.c new file +2
-0+{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque;
peds? ped_state? (I'm not sure pms <-> PCIExampleDevState is obvious; I'm not sure either whether my suggestions are better :)
hw/pci/pci_example.c line 55
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+PCIDevice *pciDev = (PCIDevice *)opaque;
pci_dev is better, I think. Also, I'd probably refer to pms->parent_obj (maybe call that one pci_device instead?) instead of casting.
hw/pci/pci_example.c line 66
Quoted 11 lines
hw/pci/pci_example.c new file +11
-0+ +if (size !=1 ) { +return ; + } + +/* compute the result */ + pms->memData = val *2 ; + +/* write the result directly to phisical memory */ +cpu_physical_memory_write (pms->dmaPhisicalBase, &pms->memData, +DMA_BUFF_SIZE );
Indentation seems off.
Quoted 5 lines
hw/pci/pci_example.c new file +5
-0+ +/* raise an IRQ to notify DMA has finished */ + pms->threwIrq =1 ; +pci_irq_assert (pciDev); +}
(...)
Quoted 2 lines
+/* do nothing because physical DMA buffer addres is onlyt set and don't need to
+ * be red */
s/addres/address/
s/onlyt/only/
s/don't/doesn't/
s/red/read/
Also, we prefer the
/*
* comment style
*/
if it can't fit on one line.
(...)
hw/pci/pci_example.c line 218
Quoted 5 lines
hw/pci/pci_example.c new file +5
-0+/*---------------------------------------------------------------------------*/ +/* PCI functions */ +/*---------------------------------------------------------------------------*/ + +/* this is called when lunching the vm with "-device <device name>" */
s/lunching/launching/
Although that's simply the normal realization process, which could also be triggered by hotplug.
hw/pci/pci_example.c line 221
Quoted 3 lines
hw/pci/pci_example.c new file +3
-0+static void pci_example_realize (PCIDevice *pciDev,Error **errp) +{ +PCIExampleDevState *d =PCI_EXAMPLE (pciDev);
Here you use "d" as a variable name, which arguably might be better than any of the suggestions above :)
hw/pci/pci_example.c line 226
Quoted 5 lines
hw/pci/pci_example.c new file +5
-0+uint8_t *pciCond = pciDev->config; + + d->threwIrq =0 ; + +/* initiallise the memory region of the CPU to the device */
s/initiallise/initialize/
hw/pci/pci_example.c line 239
Quoted 13 lines
hw/pci/pci_example.c new file +13
-0+memory_region_init_io (&d->mmio,OBJECT (d), &pci_example_mmio_ops, d, +"pci-example-mmio" ,EXAMPLE_MMIO_SIZE ); + +memory_region_init_io (&d->portio,OBJECT (d), &pci_example_pio_ops, d, +"pci-example-portio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->irqio,OBJECT (d), &pci_example_irqio_ops, d, +"pci-example-irqio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->dmaio,OBJECT (d), &pci_example_dma_ops, d, +"pci-example-dma-base" ,EXAMPLE_MMIO_SIZE ); + +/* alocate BARs */
s/alocate/allocate/
Quoted 5 lines
hw/pci/pci_example.c new file +5
-0+pci_register_bar (pciDev,0 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->mmio); +pci_register_bar (pciDev,1 ,PCI_BASE_ADDRESS_SPACE_IO , &d->portio); +pci_register_bar (pciDev,2 ,PCI_BASE_ADDRESS_SPACE_IO , &d->irqio); +pci_register_bar (pciDev,3 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->dmaio); +
hw/pci/pci_example.c line 254
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+/* give interrupt support. +* a pci device has 4 pin for interrupt, here we use pin A */ +pci_config_set_interrupt_pin (pciCond,1 ); +} + + +/* the destructor of pci_example_realize() */ +static void pci_example_uninit (PCIDevice *dev) +{ +/* unregister BARs and other stuff */
Shouldn't the function actually do it, then? :)
Quoted 17 lines
hw/pci/pci_example.c new file +17
-0+} + + +/* class constructor */ +static void pci_example_class_init (ObjectClass *klass,void *data) +{ +/* sort of dynamic cast */ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *k =PCI_DEVICE_CLASS (klass); + + k->realize = pci_example_realize; + k->exit = pci_example_uninit; + +/* some regular IDs in HEXA */ + k->vendor_id =PCI_VENDOR_ID_REDHAT ; + k->device_id =PCI_DEVICE_ID_REDHAT_TEST ; + k->class_id =PCI_CLASS_OTHERS ;
hw/pci/pci_example.c line 281
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ +/* set the device bitmap category */ +set_bit (DEVICE_CATEGORY_MISC , dc->categories); + + k->revision =0x00 ; + dc->desc ="PCI Example Device" ; +} + +/*---------------------------------------------------------------------------*/ +/* QEMU overhead */
Lol :)
Quoted 8 lines
hw/pci/pci_example.c new file +8
-0+/*---------------------------------------------------------------------------*/ + + +/* Contains all the informations of the device we are creating. +* class_init will be called when we are defining our device. */ +static const TypeInfo pci_example_info = { + .name =TYPE_PCI_EXAMPLE , + .parent =TYPE_PCI_DEVICE ,
hw/pci/pci_example.c line 299
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ .instance_size = sizeof(PCIExampleDevState), + .class_init = pci_example_class_init, + .interfaces = (InterfaceInfo []) { + {INTERFACE_CONVENTIONAL_PCI_DEVICE }, + { }, + }, +}; + + +/* function called before the qemu main it will define our device */
"Define our device type; this is done during startup of QEMU"
?
hw/pci/pci_example.c line 305
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+static void pci_example_register_types (void ) +{ +type_register_static (&pci_example_info); +} + +/* make qemu register our device at qemu-booting */
I don't think you need this comment here as well (the type_init and the function kind of form one logical unit.)
hw/pci/pci_example.c line 309
Quoted 4 lines
hw/pci/pci_example.c new file +4
-0+type_init (pci_example_register_types) + + +
I think the basic structure looks sound; I've focused on style questions as this is supposed to be an example for others to copy from. I'll leave the pci details to people more versed in pci :)
Quoted 2 lines · Cornelia Huck
On Tue, 28 Aug 2018 10:24:26 +0300
Yoni Bettan <[email protected]> wrote:
Thanks you for your review!
Quoted 6 lines
Quoted 3 lines
- this is a simple example of how to write a pci device that supports portio, mmio, irq and dma
Do you also plan to add example code for MSI(-X)?
[Not just asking because s390 only supports MSI-X. Honestly :)]
For now my main focus is to finish the virtIO device ASAP, so adding MSI-X support is in low priority.
But if it is a deal breaker for this patch to be accepted I will add it.
In both cases I have no problem to add MSI-X support later because the main purpose is to have an example device describing "the right way", so, after you bring it to my attention in my opinion, MSI-X support should be added.
I will mention that subject on V2 and see what others think about it.
Quoted 20 lines
Quoted 8 lines
Signed-off-by Yoni Bettan <[email protected]>--- hw/pci/Makefile.objs | 1 + hw/pci/pci_example.c | 309 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+)create mode 100644
hw/pci/pci_example.c(...)
Quoted 10 lines
hw/pci/pci_example.c new file +4
-0@@ -0,0 +1,309 @@ +#include "qemu/osdep.h" +#include "hw/pci/pci.h" + +#define TYPE_PCI_EXAMPLE "pci-example"
Quoted 8 lines
hw/pci/pci_example.c line 10
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+ +#define PCI_EXAMPLE (obj) \ + OBJECT_CHECK(PCIExampleDevState, (obj), TYPE_PCI_EXAMPLE) + + +#define ERROR -1It's probably not a good idea to obfuscate the return code. Either return -1, or a proper error code.
You are right, this will be repaired.
In addition another problem here is that a negative value is returned from functions that returns uint64_t so I will need to repair it anyway.
Quoted 17 lines
Quoted 17 lines
hw/pci/pci_example.c new file +16
-0+#define BLOCK_SIZE 64 +#define EXAMPLE_MMIO_SIZE BLOCK_SIZE +#define EXAMPLE_PIO_SIZE BLOCK_SIZE +#define DMA_BUFF_SIZE 4096 + +/*---------------------------------------------------------------------------*/ +/* PCI Struct */ +/*---------------------------------------------------------------------------*/ + +typedef struct PCIExampleDevState { + /*< private >*/ + PCIDevice parent_obj; + /*< public >*/ + + /* memory region */ + MemoryRegion portio;
Quoted 8 lines
hw/pci/pci_example.c line 32
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+MemoryRegion mmio; +MemoryRegion irqio; +MemoryRegion dmaio; + +/* data registers */ +uint64_t memData, ioData, dmaPhisicalBase;I think we want this to be
mem_data,io_data, anddma_physical_baseinstead.
You are right, after reading https://github.com/portante/qemu/blob/master/CODING_STYLE I have notice that and this will be updated in V2.
Quoted 6 lines
hw/pci/pci_example.c line 36
Quoted 5 lines
hw/pci/pci_example.c new file +4
-0+ +qemu_irq irq; +/* for the driver to determine if this device caused the interrupt */ +uint64_t threwIrq;Same here (
threw_irq).
Same as above.
Quoted 12 lines
Quoted 12 lines
hw/pci/pci_example.c new file +11
-0+ +} PCIExampleDevState; + + +/*---------------------------------------------------------------------------*/ +/* Read/Write functions */ +/*---------------------------------------------------------------------------*/ + +/* do nothing because the mmio read is done from DMA buffer */ +static uint64_t pci_example_mmio_read (void *opaque,hwaddr addr,unsigned size) +{
Quoted 8 lines
hw/pci/pci_example.c line 52
Quoted 5 lines
hw/pci/pci_example.c new file +5
-0+return ERROR ; +} + +static void +pci_example_mmio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size)I think our preferred style is to keep return value and function name on the same line and add line breaks in the parameter list, if needed. (Some more occurrences further down.)
No problem, I prefer this style to.
Quoted 5 lines
hw/pci/pci_example.c line 54
Quoted 3 lines
hw/pci/pci_example.c new file +2
-0+{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque;peds?
ped_state? (I'm not sure pms <-> PCIExampleDevState is obvious; I'm not sure either whether my suggestions are better :)
This device originally was called PCIMulDevState since it just multiply a number, before submitting the patch I changed it to PCIExampleDevState but forgot to update the variables names :) I will repair that.
Quoted 4 lines
hw/pci/pci_example.c line 55
Quoted 2 lines
hw/pci/pci_example.c new file +1
-0+PCIDevice *pciDev = (PCIDevice *)opaque;
pci_devis better, I think. Also, I'd probably refer topms->parent_obj(maybe call that onepci_deviceinstead?) instead of casting.
I will update the name to fit to the coding style. About referencing to parent_obj I need to look again at the code to understand why I did it that way.
Quoted 13 lines
hw/pci/pci_example.c line 66
Quoted 12 lines
hw/pci/pci_example.c new file +11
-0+ +if (size !=1 ) { +return ; + } + +/* compute the result */ + pms->memData = val *2 ; + +/* write the result directly to phisical memory */ +cpu_physical_memory_write (pms->dmaPhisicalBase, &pms->memData, +DMA_BUFF_SIZE );Indentation seems off.
How would you have done it otherwise ? this is a simple "Enter" when 80 characters are reached.
Quoted 8 lines
Quoted 6 lines
hw/pci/pci_example.c new file +5
-0+ +/* raise an IRQ to notify DMA has finished */ + pms->threwIrq =1 ; +pci_irq_assert (pciDev); +}(...)
Quoted 6 lines
Quoted 2 lines
+/* do nothing because physical DMA buffer addres is onlyt set and don't need to
+ * be red */s/addres/address/
s/onlyt/only/
s/don't/doesn't/
s/red/read/
Sorry for that, I didn't run spell check.
Quoted 10 lines
Also, we prefer the
/*
* comment style
*/if it can't fit on one line.
(...)
Got it.
Quoted 10 lines
hw/pci/pci_example.c line 218
Quoted 6 lines
hw/pci/pci_example.c new file +5
-0+/*---------------------------------------------------------------------------*/ +/* PCI functions */ +/*---------------------------------------------------------------------------*/ + +/* this is called when lunching the vm with "-device <device name>" */ s/lunching/launching/
Although that's simply the normal realization process, which could also be triggered by hotplug.
I will add this in the comment.
Quoted 6 lines
hw/pci/pci_example.c line 221
Quoted 4 lines
hw/pci/pci_example.c new file +3
-0+static void pci_example_realize (PCIDevice *pciDev,Error **errp) +{ +PCIExampleDevState *d =PCI_EXAMPLE (pciDev);Here you use "d" as a variable name, which arguably might be better than any of the suggestions above :)
I will make it generic and give a purposeful name :)
Quoted 7 lines
hw/pci/pci_example.c line 226
Quoted 6 lines
hw/pci/pci_example.c new file +5
-0+uint8_t *pciCond = pciDev->config; + + d->threwIrq =0 ; + +/* initiallise the memory region of the CPU to the device */ s/initiallise/initialize/
I will run set spell on the whole file.
Quoted 15 lines
Quoted 4 lines
hw/pci/pci_example.c new file +3
-0+memory_region_init_io (&d->mmio,OBJECT (d), &pci_example_mmio_ops, d, +"pci-example-mmio" ,EXAMPLE_MMIO_SIZE ); +hw/pci/pci_example.c line 239
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+memory_region_init_io (&d->portio,OBJECT (d), &pci_example_pio_ops, d, +"pci-example-portio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->irqio,OBJECT (d), &pci_example_irqio_ops, d, +"pci-example-irqio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->dmaio,OBJECT (d), &pci_example_dma_ops, d, +"pci-example-dma-base" ,EXAMPLE_MMIO_SIZE ); + +/* alocate BARs */ s/alocate/allocate/
Same.
Quoted 17 lines
Quoted 6 lines
hw/pci/pci_example.c new file +5
-0+pci_register_bar (pciDev,0 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->mmio); +pci_register_bar (pciDev,1 ,PCI_BASE_ADDRESS_SPACE_IO , &d->portio); +pci_register_bar (pciDev,2 ,PCI_BASE_ADDRESS_SPACE_IO , &d->irqio); +pci_register_bar (pciDev,3 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->dmaio); +hw/pci/pci_example.c line 254
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+/* give interrupt support. +* a pci device has 4 pin for interrupt, here we use pin A */ +pci_config_set_interrupt_pin (pciCond,1 ); +} + + +/* the destructor of pci_example_realize() */ +static void pci_example_uninit (PCIDevice *dev) +{ +/* unregister BARs and other stuff */ Shouldn't the function actually do it, then? :)
Yes it does, it was a prototype but I will make it full for submission. Thanks for pointing on this one.
Quoted 50 lines
Quoted 18 lines
hw/pci/pci_example.c new file +17
-0+} + + +/* class constructor */ +static void pci_example_class_init (ObjectClass *klass,void *data) +{ +/* sort of dynamic cast */ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *k =PCI_DEVICE_CLASS (klass); + + k->realize = pci_example_realize; + k->exit = pci_example_uninit; + +/* some regular IDs in HEXA */ + k->vendor_id =PCI_VENDOR_ID_REDHAT ; + k->device_id =PCI_DEVICE_ID_REDHAT_TEST ; + k->class_id =PCI_CLASS_OTHERS ;hw/pci/pci_example.c line 281
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ +/* set the device bitmap category */ +set_bit (DEVICE_CATEGORY_MISC , dc->categories); + + k->revision =0x00 ; + dc->desc ="PCI Example Device" ; +} + +/*---------------------------------------------------------------------------*/ +/* QEMU overhead */ Lol :)
Quoted 8 lines
hw/pci/pci_example.c new file +8
-0+/*---------------------------------------------------------------------------*/ + + +/* Contains all the informations of the device we are creating. +* class_init will be called when we are defining our device. */ +static const TypeInfo pci_example_info = { + .name =TYPE_PCI_EXAMPLE , + .parent =TYPE_PCI_DEVICE ,hw/pci/pci_example.c line 299
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ .instance_size = sizeof(PCIExampleDevState), + .class_init = pci_example_class_init, + .interfaces = (InterfaceInfo []) { + {INTERFACE_CONVENTIONAL_PCI_DEVICE }, + { }, + }, +}; + + +/* function called before the qemu main it will define our device */ "Define our device type; this is done during startup of QEMU"
Quoted 1 line
?
This is a pretty good suggestion!
Quoted 9 lines
hw/pci/pci_example.c line 305
Quoted 7 lines
hw/pci/pci_example.c new file +6
-0+static void pci_example_register_types (void ) +{ +type_register_static (&pci_example_info); +} + +/* make qemu register our device at qemu-booting */ I don't think you need this comment here as well (the
type_initand the function kind of form one logical unit.)
No problem, I will remove it.
Quoted 8 lines
hw/pci/pci_example.c line 309
Quoted 5 lines
hw/pci/pci_example.c new file +4
-0+type_init (pci_example_register_types) + + +I think the basic structure looks sound; I've focused on style questions as this is supposed to be an example for others to copy from. I'll leave the pci details to people more versed in pci :)
Thanks again for this review!
Yoni
Hi,
Thanks for the patch.
Quoted 2 lines · Yoni Bettan
- this is a simple example of how to write a pci device that supports portio, mmio, irq and dma
Can we have some documentation on what are the goals of the example device, and what exactly it does?
Quoted 13 lines
Signed-off-by Yoni Bettan <[email protected]>--- hw/pci/Makefile.objs | 1 + hw/pci/pci_example.c | 309 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+) create mode 100644 hw/pci/pci_example.chw/pci/Makefile.objs +0
-0
hw/pci/Makefile.objs common-obj-$(CONFIG_PCI) += shpc.o · line 7
Quoted 4 lines
hw/pci/Makefile.objs +1
-0@@ -4,6 +4,7 @@ common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pci_example.o
I am wondering how we can guarantee nobody will break the example code while not including it by default on production builds. Maybe disabling the device by default, adding a ./configure --enable-examples option, and adding it to one of the test cases on .travis.yml?
hw/pci/pci_example.c line 3
Quoted 12 lines
hw/pci/Makefile.objs +0
-0common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci_example.c new file +3
-0@@ -0,0 +1,309 @@ +#include "qemu/osdep.h" +#include "hw/pci/pci.h" +
Being an example device, it would be nice to explain what these two macros are used for.
hw/pci/pci_example.c line 9
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+#define TYPE_PCI_EXAMPLE "pci-example" + +#define PCI_EXAMPLE (obj) \ + OBJECT_CHECK(PCIExampleDevState, (obj), TYPE_PCI_EXAMPLE) + +
One line descriptions of the meaning of the macros below would be nice to have.
hw/pci/pci_example.c line 10
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+#define ERROR -1
I'm not sure we need this macro. Additional comments below where the macro is actually used.
hw/pci/pci_example.c line 13
Quoted 3 lines
hw/pci/pci_example.c new file +3
-0+#define BLOCK_SIZE 64 +#define EXAMPLE_MMIO_SIZE BLOCK_SIZE +#define EXAMPLE_PIO_SIZE BLOCK_SIZE
I don't see BLOCK_SIZE being used anywhere in the code. Wouldn't it be more readable if you just did:
#define EXAMPLE_MMIO_SIZE 64 #define EXAMPLE_PIO_SIZE 64
hw/pci/pci_example.c line 14
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+#define DMA_BUFF_SIZE 4096
I suggest DMA_BUF_SIZE, as "buf" is a more usual abbreviation for "buffer".
hw/pci/pci_example.c line 22
Quoted 8 lines
hw/pci/pci_example.c new file +8
-0+ +/*---------------------------------------------------------------------------*/ +/* PCI Struct */ +/*---------------------------------------------------------------------------*/ + +typedef struct PCIExampleDevState { + /*< private >*/ + PCIDevice parent_obj;
One line description of what parent_obj is, or a pointer to QOM documentation explaining it would be nice.
hw/pci/pci_example.c line 25
Quoted 3 lines
hw/pci/pci_example.c new file +3
-0+/*< public >*/ + +/* memory region */
This comment seems redundant: the words "memory region" are already spelled 4 times below.
hw/pci/pci_example.c line 32
Quoted 7 lines
hw/pci/pci_example.c new file +7
-0+MemoryRegion portio; +MemoryRegion mmio; +MemoryRegion irqio; +MemoryRegion dmaio; + +/* data registers */ +uint64_t memData, ioData, dmaPhisicalBase;
Can we have a one-line description of each of the registers?
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+ +qemu_irq irq; +/* for the driver to determine if this device caused the interrupt */ +uint64_t threwIrq; + +} PCIExampleDevState;
hw/pci/pci_example.c line 48
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ + +/*---------------------------------------------------------------------------*/ +/* Read/Write functions */ +/*---------------------------------------------------------------------------*/ + +/* do nothing because the mmio read is done from DMA buffer */ +static uint64_t pci_example_mmio_read (void *opaque,hwaddr addr,unsigned size) +{ +return ERROR ;
You can't return a negative value from a uint64_t function. What exactly you are trying to communicate to the caller by returning 0xffffffffffffffff here?
Same problem below[5].
hw/pci/pci_example.c line 54
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+} + +static void +pci_example_mmio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque;
You can write this as:
PCIExampleDevState *pms = opaque;
Same below[1].
hw/pci/pci_example.c line 55
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+PCIDevice *pciDev = (PCIDevice *)opaque;
I suggest using PCI_DEVICE(pms) here and below[2].
hw/pci/pci_example.c line 59
Quoted 4 lines
hw/pci/pci_example.c new file +4
-0+ +if (size !=1 ) { +return ; + }
Why you want to support only 1-byte writes?
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+ +/* compute the result */ + pms->memData = val *2 ; + +/* write the result directly to phisical memory */ + cpu_physical_memory_write(pms->dmaPhisicalBase, &pms->memData,
hw/pci/pci_example.c line 75
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ DMA_BUFF_SIZE); + +/* raise an IRQ to notify DMA has finished */ + pms->threwIrq =1 ; +pci_irq_assert (pciDev); +} + +static uint64_t pci_example_pio_read (void *opaque,hwaddr addr,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque;
[1]
hw/pci/pci_example.c line 78
Quoted 3 lines
hw/pci/pci_example.c new file +3
-0+ +if (size !=1 ) { +return ERROR ;
Same problem as pci_example_mmio_read() above.
hw/pci/pci_example.c line 79
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+ }
Same as above: why you want to support only 1-byte reads? Same below[3].
hw/pci/pci_example.c line 87
Quoted 8 lines
hw/pci/pci_example.c new file +8
-0+ +return pms->ioData; +} + +static void +pci_example_pio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque;
[1]
hw/pci/pci_example.c line 88
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+PCIDevice *pciDev = (PCIDevice *)opaque;
[2]
hw/pci/pci_example.c line 90
Quoted 2 lines
hw/pci/pci_example.c new file +2
-0+ +if (size !=1 ) {
[3]
hw/pci/pci_example.c line 103
Quoted 13 lines
hw/pci/pci_example.c new file +13
-0+return ; + } + + pms->ioData = val *2 ; + pms->threwIrq =1 ; +pci_irq_assert (pciDev); +} + +static uint64_t pci_example_irqio_read (void *opaque,hwaddr addr,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; + +if (size !=1 ) {
[3]
hw/pci/pci_example.c line 104
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+return ERROR ;
[5]
hw/pci/pci_example.c line 116
Quoted 12 lines
hw/pci/pci_example.c new file +12
-0+ } + +return pms->threwIrq; +} + +static void +pci_example_irqio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; +PCIDevice *pciDev = (PCIDevice *)opaque; + +if (size !=1 ) {
[3]
Quoted 9 lines
hw/pci/pci_example.c new file +9
-0+return ; + } + +/* give the ability to assert IRQ , we will use it only to deassert IRQ */ +if (val) { +pci_irq_assert (pciDev); + pms->threwIrq =1 ; + }else { + pms->threwIrq =0 ;
hw/pci/pci_example.c line 135
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+pci_irq_deassert (pciDev); + } +} + +/* do nothing because physical DMA buffer addres is onlyt set and don't need to +* be red */ +static uint64_t +pci_example_dma_base_read (void *opaque,hwaddr addr,unsigned size) +{ +return ERROR ;
[5]
Quoted 32 lines
hw/pci/pci_example.c new file +32
-0+} + +static void +pci_example_dma_base_write (void *opaque,hwaddr addr,uint64_t val, +unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; + +if (size !=4 ) { +return ; + } + +/* notify the device about the physical address of the DMA buffer that the +* driver has allocated */ +switch (addr) { +/* lower bytes */ +case (0 ): + pms->dmaPhisicalBase &=0xffffffff00000000 ; +break ; + +/* upper bytes */ +case (4 ): + val <<=32 ; + pms->dmaPhisicalBase &=0x00000000ffffffff ; +break ; + } + + pms->dmaPhisicalBase |= val; +} + +/*---------------------------------------------------------------------------*/ +/* PCI region ops */
hw/pci/pci_example.c line 177
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+/*---------------------------------------------------------------------------*/ + +/* callback called when memory region representing the MMIO space is accessed */ +static const MemoryRegionOps pci_example_mmio_ops = { + .read = pci_example_mmio_read, + .write = pci_example_mmio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 ,
Now I see you set min/max access size. Maybe the size checks above[3] can be asserts?
If I read the documentation correctly, a 64-bit write will become a series of 1-byte writes, and the device behavior might be confusing. Supporting 64-bit writes would probably make it more intuitive.
hw/pci/pci_example.c line 188
Quoted 11 lines
hw/pci/pci_example.c new file +11
-0+ }, +}; + +/* callback called when memory region representing the PIO space is accessed */ +static const MemoryRegionOps pci_example_pio_ops = { + .read = pci_example_pio_read, + .write = pci_example_pio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 ,
[3]
hw/pci/pci_example.c line 199
Quoted 11 lines
hw/pci/pci_example.c new file +11
-0+ }, +}; + +/* callback called when memory region representing the IRQ space is accessed */ +static const MemoryRegionOps pci_example_irqio_ops = { + .read = pci_example_irqio_read, + .write = pci_example_irqio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 ,
[3]
hw/pci/pci_example.c line 210
Quoted 11 lines
hw/pci/pci_example.c new file +11
-0+ }, +}; + +/* callback called when memory region representing the DMA space is accessed */ +static const MemoryRegionOps pci_example_dma_ops = { + .read = pci_example_dma_base_read, + .write = pci_example_dma_base_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =4 , + .max_access_size =4 ,
[3]
Quoted 4 lines
hw/pci/pci_example.c new file +4
-0+ }, +}; + +/*---------------------------------------------------------------------------*/
hw/pci/pci_example.c line 224
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+/* PCI functions */ +/*---------------------------------------------------------------------------*/ + +/* this is called when lunching the vm with "-device <device name>" */ +static void pci_example_realize (PCIDevice *pciDev,Error **errp) +{ +PCIExampleDevState *d =PCI_EXAMPLE (pciDev); +uint8_t *pciCond = pciDev->config; + + d->threwIrq =0 ;
Is this really necessary?
hw/pci/pci_example.c line 226
Quoted 2 lines
hw/pci/pci_example.c new file +2
-0+ +/* initiallise the memory region of the CPU to the device */
What does "memory region of the CPU" means?
hw/pci/pci_example.c line 237
Quoted 11 lines
hw/pci/pci_example.c new file +11
-0+memory_region_init_io (&d->mmio,OBJECT (d), &pci_example_mmio_ops, d, +"pci-example-mmio" ,EXAMPLE_MMIO_SIZE ); + +memory_region_init_io (&d->portio,OBJECT (d), &pci_example_pio_ops, d, +"pci-example-portio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->irqio,OBJECT (d), &pci_example_irqio_ops, d, +"pci-example-irqio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->dmaio,OBJECT (d), &pci_example_dma_ops, d, +"pci-example-dma-base" ,EXAMPLE_MMIO_SIZE );
Why you chose to register 64-byte regions instead of a smaller 1, 2, 4, or 8-byte region?
Quoted 5 lines
hw/pci/pci_example.c new file +5
-0+ +/* alocate BARs */ +pci_register_bar (pciDev,0 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->mmio); +pci_register_bar (pciDev,1 ,PCI_BASE_ADDRESS_SPACE_IO , &d->portio); +pci_register_bar (pciDev,2 ,PCI_BASE_ADDRESS_SPACE_IO , &d->irqio);
hw/pci/pci_example.c line 252
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+pci_register_bar (pciDev,3 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->dmaio); + +/* give interrupt support. +* a pci device has 4 pin for interrupt, here we use pin A */ +pci_config_set_interrupt_pin (pciCond,1 ); +} + + +/* the destructor of pci_example_realize() */ +static void pci_example_uninit (PCIDevice *dev)
I suggest calling it pci_example_exit, as the callback name is PCIDeviceClass::exit.
hw/pci/pci_example.c line 254
Quoted 2 lines
hw/pci/pci_example.c new file +2
-0+{ +/* unregister BARs and other stuff */
If there's nothing the device needs to do on unrealize, you don't need an unrealize function at all.
Quoted 4 lines
hw/pci/pci_example.c new file +4
-0+} + + +/* class constructor */
hw/pci/pci_example.c line 268
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+static void pci_example_class_init (ObjectClass *klass,void *data) +{ +/* sort of dynamic cast */ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *k =PCI_DEVICE_CLASS (klass); + + k->realize = pci_example_realize; + k->exit = pci_example_uninit; + +/* some regular IDs in HEXA */
What "HEXA" means here?
hw/pci/pci_example.c line 270
Quoted 2 lines
hw/pci/pci_example.c new file +2
-0+ k->vendor_id =PCI_VENDOR_ID_REDHAT ; + k->device_id =PCI_DEVICE_ID_REDHAT_TEST ;
Hmm, this is the device ID of pci-testdev. We can't just reuse it. Maybe it would be appropriate to use an ID on the 0x10f0-0x10ff range? I assume it would be appropriate only if the device is not compiled in by default.
I also wonder if we we could make pci-test our example device instead of adding a new one. What I really miss here is some guide to point people to known-good examples of device emulation code. We could do that with a new example device, or choose some existing good examples and make them better documented.
Quoted 13 lines
hw/pci/pci_example.c new file +13
-0+ k->class_id =PCI_CLASS_OTHERS ; + +/* set the device bitmap category */ +set_bit (DEVICE_CATEGORY_MISC , dc->categories); + + k->revision =0x00 ; + dc->desc ="PCI Example Device" ; +} + +/*---------------------------------------------------------------------------*/ +/* QEMU overhead */ +/*---------------------------------------------------------------------------*/ +
hw/pci/pci_example.c line 293
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ +/* Contains all the informations of the device we are creating. +* class_init will be called when we are defining our device. */ +static const TypeInfo pci_example_info = { + .name =TYPE_PCI_EXAMPLE , + .parent =TYPE_PCI_DEVICE , + .instance_size =sizeof (PCIExampleDevState), + .class_init = pci_example_class_init, + .interfaces = (InterfaceInfo []) { + {INTERFACE_CONVENTIONAL_PCI_DEVICE },
A comment explaining why we have INTERFACE_CONVENTIONAL_PCI_DEVICE might be useful.
Quoted 19 lines
hw/pci/pci_example.c new file +16
-0+ { }, + }, +}; + + +/* function called before the qemu main it will define our device */ +static void pci_example_register_types (void ) +{ +type_register_static (&pci_example_info); +} + +/* make qemu register our device at qemu-booting */ +type_init (pci_example_register_types) + + +Signature · 1 line
2.17.1
Signature · 1 line
Eduardo
Quoted 9 lines · Eduardo Habkost
Hi,
Thanks for the patch.
Quoted 2 lines · Yoni Bettan
- this is a simple example of how to write a pci device that supports portio, mmio, irq and dma
Can we have some documentation on what are the goals of the example device, and what exactly it does?
I replied to the patch with explanation about the device and its purpose, I will add it to the commit message in V2.
Quoted 9 lines
Quoted 9 lines
Signed-off-by Yoni Bettan <[email protected]>--- hw/pci/Makefile.objs | 1 + hw/pci/pci_example.c | 309 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+)create mode 100644
hw/pci/pci_example.c
Quoted 14 lines
hw/pci/Makefile.objs common-obj-$(CONFIG_PCI) += shpc.o · line 7
Quoted 9 lines
hw/pci/Makefile.objs +1
-0@@ -4,6 +4,7 @@ common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pci_example.oI am wondering how we can guarantee nobody will break the example code while not including it by default on production builds. Maybe disabling the device by default, adding a ./configure --enable-examples option, and adding it to one of the test cases on .travis.yml?
I will add this as a question in V2 to here what is the opinion of the other about this.
Quoted 15 lines
hw/pci/pci_example.c line 3
Quoted 13 lines
hw/pci/Makefile.objs +0
-0common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci_example.c new file +3
-0@@ -0,0 +1,309 @@ +#include "qemu/osdep.h" +#include "hw/pci/pci.h" +Being an example device, it would be nice to explain what these two macros are used for.
Good idea, I will do it.
Quoted 11 lines
hw/pci/pci_example.c line 9
Quoted 8 lines
hw/pci/pci_example.c new file +6
-0+#define TYPE_PCI_EXAMPLE "pci-example" + +#define PCI_EXAMPLE (obj) \ + OBJECT_CHECK(PCIExampleDevState, (obj), TYPE_PCI_EXAMPLE) + +One line descriptions of the meaning of the macros below would be nice to have.
Quoted 3 lines
hw/pci/pci_example.c line 10
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+#define ERROR -1I'm not sure we need this macro. Additional comments below where the macro is actually used.
I will remove this macro.
Quoted 10 lines
hw/pci/pci_example.c line 13
Quoted 5 lines
hw/pci/pci_example.c new file +3
-0+#define BLOCK_SIZE 64 +#define EXAMPLE_MMIO_SIZE BLOCK_SIZE +#define EXAMPLE_PIO_SIZE BLOCK_SIZEI don't see
BLOCK_SIZEbeing used anywhere in the code. Wouldn't it be more readable if you just did:#define EXAMPLE_MMIO_SIZE 64 #define EXAMPLE_PIO_SIZE 64
I will remove it.
Quoted 4 lines
hw/pci/pci_example.c line 14
Quoted 2 lines
hw/pci/pci_example.c new file +1
-0+#define DMA_BUFF_SIZE 4096I suggest
DMA_BUF_SIZE, as "buf" is a more usual abbreviation for "buffer".
No problem.
Quoted 11 lines
hw/pci/pci_example.c line 22
Quoted 9 lines
hw/pci/pci_example.c new file +8
-0+ +/*---------------------------------------------------------------------------*/ +/* PCI Struct */ +/*---------------------------------------------------------------------------*/ + +typedef struct PCIExampleDevState { + /*< private >*/ + PCIDevice parent_obj;One line description of what
parent_objis, or a pointer to QOM documentation explaining it would be nice.
Good idea.
Quoted 6 lines
hw/pci/pci_example.c line 25
Quoted 4 lines
hw/pci/pci_example.c new file +3
-0+/*< public >*/ + +/* memory region */ This comment seems redundant: the words "memory region" are already spelled 4 times below.
I agree, I will remove it.
Quoted 9 lines
hw/pci/pci_example.c line 32
Quoted 8 lines
hw/pci/pci_example.c new file +7
-0+MemoryRegion portio; +MemoryRegion mmio; +MemoryRegion irqio; +MemoryRegion dmaio; + +/* data registers */ +uint64_t memData, ioData, dmaPhisicalBase;Can we have a one-line description of each of the registers?
Sure.
Quoted 21 lines
Quoted 7 lines
hw/pci/pci_example.c new file +6
-0+ +qemu_irq irq; +/* for the driver to determine if this device caused the interrupt */ +uint64_t threwIrq; + +} PCIExampleDevState;hw/pci/pci_example.c line 48
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ + +/*---------------------------------------------------------------------------*/ +/* Read/Write functions */ +/*---------------------------------------------------------------------------*/ + +/* do nothing because the mmio read is done from DMA buffer */ +static uint64_t pci_example_mmio_read (void *opaque,hwaddr addr,unsigned size) +{ +return ERROR ;You can't return a negative value from a
uint64_tfunction. What exactly you are trying to communicate to the caller by returning0xffffffffffffffffhere?
Quoted 1 line
Same problem below[5].
You are right, this is a bug, I will solve this issue.
Quoted 9 lines
hw/pci/pci_example.c line 54
Quoted 7 lines
hw/pci/pci_example.c new file +6
-0+} + +static void +pci_example_mmio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque;You can write this as:
PCIExampleDevState *pms = opaque;
Implicit casting, yes... I will remove the explicit cast and use PCI_EXAMPLE_DEVICE(opaque) instead.
Quoted 6 lines
Same below[1].
hw/pci/pci_example.c line 55
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+PCIDevice *pciDev = (PCIDevice *)opaque;I suggest using
PCI_DEVICE(pms)here and below[2].
same.
Quoted 7 lines
hw/pci/pci_example.c line 59
Quoted 6 lines
hw/pci/pci_example.c new file +4
-0+ +if (size !=1 ) { +return ; + }Why you want to support only 1-byte writes?
I think this is enough for a simple example device.
Quoted 19 lines
Quoted 7 lines
hw/pci/pci_example.c new file +6
-0+ +/* compute the result */ + pms->memData = val *2 ; + +/* write the result directly to phisical memory */ + cpu_physical_memory_write(pms->dmaPhisicalBase, &pms->memData,hw/pci/pci_example.c line 75
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+ DMA_BUFF_SIZE); + +/* raise an IRQ to notify DMA has finished */ + pms->threwIrq =1 ; +pci_irq_assert (pciDev); +} + +static uint64_t pci_example_pio_read (void *opaque,hwaddr addr,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque;[1]
Quoted 4 lines
hw/pci/pci_example.c line 78
Quoted 3 lines
hw/pci/pci_example.c new file +3
-0+ +if (size !=1 ) { +return ERROR ;Same problem as
pci_example_mmio_read()above.
same.
Quoted 120 lines
hw/pci/pci_example.c line 79
Quoted 2 lines
hw/pci/pci_example.c new file +1
-0+ }Same as above: why you want to support only 1-byte reads? Same below[3].
hw/pci/pci_example.c line 87
Quoted 8 lines
hw/pci/pci_example.c new file +8
-0+ +return pms->ioData; +} + +static void +pci_example_pio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque;[1]
hw/pci/pci_example.c line 88
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+PCIDevice *pciDev = (PCIDevice *)opaque;[2]
hw/pci/pci_example.c line 90
Quoted 2 lines
hw/pci/pci_example.c new file +2
-0+ +if (size !=1 ) {[3]
hw/pci/pci_example.c line 103
Quoted 13 lines
hw/pci/pci_example.c new file +13
-0+return ; + } + + pms->ioData = val *2 ; + pms->threwIrq =1 ; +pci_irq_assert (pciDev); +} + +static uint64_t pci_example_irqio_read (void *opaque,hwaddr addr,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; + +if (size !=1 ) {[3]
hw/pci/pci_example.c line 104
Quoted 1 line
hw/pci/pci_example.c new file +1
-0+return ERROR ;[5]
hw/pci/pci_example.c line 116
Quoted 12 lines
hw/pci/pci_example.c new file +12
-0+ } + +return pms->threwIrq; +} + +static void +pci_example_irqio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; +PCIDevice *pciDev = (PCIDevice *)opaque; + +if (size !=1 ) {[3]
Quoted 9 lines
hw/pci/pci_example.c new file +9
-0+return ; + } + +/* give the ability to assert IRQ , we will use it only to deassert IRQ */ +if (val) { +pci_irq_assert (pciDev); + pms->threwIrq =1 ; + }else { + pms->threwIrq =0 ;hw/pci/pci_example.c line 135
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+pci_irq_deassert (pciDev); + } +} + +/* do nothing because physical DMA buffer addres is onlyt set and don't need to +* be red */ +static uint64_t +pci_example_dma_base_read (void *opaque,hwaddr addr,unsigned size) +{ +return ERROR ;[5]
Quoted 32 lines
hw/pci/pci_example.c new file +32
-0+} + +static void +pci_example_dma_base_write (void *opaque,hwaddr addr,uint64_t val, +unsigned size) +{ +PCIExampleDevState *pms = (PCIExampleDevState *)opaque; + +if (size !=4 ) { +return ; + } + +/* notify the device about the physical address of the DMA buffer that the +* driver has allocated */ +switch (addr) { +/* lower bytes */ +case (0 ): + pms->dmaPhisicalBase &=0xffffffff00000000 ; +break ; + +/* upper bytes */ +case (4 ): + val <<=32 ; + pms->dmaPhisicalBase &=0x00000000ffffffff ; +break ; + } + + pms->dmaPhisicalBase |= val; +} + +/*---------------------------------------------------------------------------*/ +/* PCI region ops */ hw/pci/pci_example.c line 177
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+/*---------------------------------------------------------------------------*/ + +/* callback called when memory region representing the MMIO space is accessed */ +static const MemoryRegionOps pci_example_mmio_ops = { + .read = pci_example_mmio_read, + .write = pci_example_mmio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 ,Now I see you set min/max access size. Maybe the size checks above[3] can be asserts?
Quoted 4 lines
If I read the documentation correctly, a 64-bit write will become a series of 1-byte writes, and the device behavior might be confusing. Supporting 64-bit writes would probably make it more intuitive.
I will check this option.
Quoted 41 lines
hw/pci/pci_example.c line 188
Quoted 13 lines
hw/pci/pci_example.c new file +11
-0+ }, +}; + +/* callback called when memory region representing the PIO space is accessed */ +static const MemoryRegionOps pci_example_pio_ops = { + .read = pci_example_pio_read, + .write = pci_example_pio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 ,[3]
hw/pci/pci_example.c line 199
Quoted 11 lines
hw/pci/pci_example.c new file +11
-0+ }, +}; + +/* callback called when memory region representing the IRQ space is accessed */ +static const MemoryRegionOps pci_example_irqio_ops = { + .read = pci_example_irqio_read, + .write = pci_example_irqio_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =1 , + .max_access_size =1 ,[3]
hw/pci/pci_example.c line 210
Quoted 11 lines
hw/pci/pci_example.c new file +11
-0+ }, +}; + +/* callback called when memory region representing the DMA space is accessed */ +static const MemoryRegionOps pci_example_dma_ops = { + .read = pci_example_dma_base_read, + .write = pci_example_dma_base_write, + .endianness =DEVICE_LITTLE_ENDIAN , + .impl = { + .min_access_size =4 , + .max_access_size =4 ,[3]
Quoted 15 lines
Quoted 4 lines
hw/pci/pci_example.c new file +4
-0+ }, +}; + +/*---------------------------------------------------------------------------*/ hw/pci/pci_example.c line 224
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+/* PCI functions */ +/*---------------------------------------------------------------------------*/ + +/* this is called when lunching the vm with "-device <device name>" */ +static void pci_example_realize (PCIDevice *pciDev,Error **errp) +{ +PCIExampleDevState *d =PCI_EXAMPLE (pciDev); +uint8_t *pciCond = pciDev->config; + + d->threwIrq =0 ;Is this really necessary?
No, I will remove it since QEMU does this automatically.k
Quoted 5 lines
hw/pci/pci_example.c line 226
Quoted 4 lines
hw/pci/pci_example.c new file +2
-0+ +/* initiallise the memory region of the CPU to the device */ What does "memory region of the CPU" means?
I will update this comment.
Quoted 15 lines
hw/pci/pci_example.c line 237
Quoted 13 lines
hw/pci/pci_example.c new file +11
-0+memory_region_init_io (&d->mmio,OBJECT (d), &pci_example_mmio_ops, d, +"pci-example-mmio" ,EXAMPLE_MMIO_SIZE ); + +memory_region_init_io (&d->portio,OBJECT (d), &pci_example_pio_ops, d, +"pci-example-portio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->irqio,OBJECT (d), &pci_example_irqio_ops, d, +"pci-example-irqio" ,EXAMPLE_PIO_SIZE ); + +memory_region_init_io (&d->dmaio,OBJECT (d), &pci_example_dma_ops, d, +"pci-example-dma-base" ,EXAMPLE_MMIO_SIZE );Why you chose to register 64-byte regions instead of a smaller 1, 2, 4, or 8-byte region?
I will add it as a FIXME for now.
Quoted 10 lines
Quoted 10 lines
hw/pci/pci_example.c new file +9
-0+ +/* alocate BARs */ +pci_register_bar (pciDev,0 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->mmio); +pci_register_bar (pciDev,1 ,PCI_BASE_ADDRESS_SPACE_IO , &d->portio); +pci_register_bar (pciDev,2 ,PCI_BASE_ADDRESS_SPACE_IO , &d->irqio); +pci_register_bar (pciDev,3 ,PCI_BASE_ADDRESS_SPACE_MEMORY , &d->dmaio); + +/* give interrupt support. +* a pci device has 4 pin for interrupt, here we use pin A */
Quoted 8 lines
hw/pci/pci_example.c line 252
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+pci_config_set_interrupt_pin (pciCond,1 ); +} + + +/* the destructor of pci_example_realize() */ +static void pci_example_uninit (PCIDevice *dev)I suggest calling it
pci_example_exit, as the callback name isPCIDeviceClass::exit.
OK.
Quoted 6 lines
hw/pci/pci_example.c line 254
Quoted 4 lines
hw/pci/pci_example.c new file +2
-0+{ +/* unregister BARs and other stuff */ If there's nothing the device needs to do on unrealize, you don't need an unrealize function at all.
The device does need to unallocate BARs, I will add it as a FIXME for now.
Quoted 16 lines
Quoted 5 lines
hw/pci/pci_example.c new file +4
-0+} + + +/* class constructor */ hw/pci/pci_example.c line 268
Quoted 10 lines
hw/pci/pci_example.c new file +10
-0+static void pci_example_class_init (ObjectClass *klass,void *data) +{ +/* sort of dynamic cast */ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *k =PCI_DEVICE_CLASS (klass); + + k->realize = pci_example_realize; + k->exit = pci_example_uninit; + +/* some regular IDs in HEXA */ What "HEXA" means here?
HEXA=Hexadecimal base - I will make it more clear.
Quoted 7 lines
hw/pci/pci_example.c line 270
Quoted 3 lines
hw/pci/pci_example.c new file +2
-0+ k->vendor_id =PCI_VENDOR_ID_REDHAT ; + k->device_id =PCI_DEVICE_ID_REDHAT_TEST ;Hmm, this is the device ID of pci-testdev. We can't just reuse it. Maybe it would be appropriate to use an ID on the 0x10f0-0x10ff range? I assume it would be appropriate only if the device is not compiled in by default.
I will update the device ID once we decide how to compile this device because as I see it there are related.
Quoted 6 lines
I also wonder if we we could make pci-test our example device instead of adding a new one. What I really miss here is some guide to point people to known-good examples of device emulation code. We could do that with a new example device, or choose some existing good examples and make them better documented.
I will check this option.
Quoted 19 lines
Quoted 19 lines
hw/pci/pci_example.c new file +17
-0+ k->class_id =PCI_CLASS_OTHERS ; + +/* set the device bitmap category */ +set_bit (DEVICE_CATEGORY_MISC , dc->categories); + + k->revision =0x00 ; + dc->desc ="PCI Example Device" ; +} + +/*---------------------------------------------------------------------------*/ +/* QEMU overhead */ +/*---------------------------------------------------------------------------*/ + + +/* Contains all the informations of the device we are creating. +* class_init will be called when we are defining our device. */ +static const TypeInfo pci_example_info = {
Quoted 8 lines
hw/pci/pci_example.c line 293
Quoted 6 lines
hw/pci/pci_example.c new file +6
-0+ .name = TYPE_PCI_EXAMPLE, + .parent = TYPE_PCI_DEVICE, + .instance_size = sizeof(PCIExampleDevState), + .class_init = pci_example_class_init, + .interfaces = (InterfaceInfo []) { + {INTERFACE_CONVENTIONAL_PCI_DEVICE },A comment explaining why we have
INTERFACE_CONVENTIONAL_PCI_DEVICEmight be useful.
No problem.
Quoted 20 lines
Quoted 20 lines
hw/pci/pci_example.c new file +16
-0+ { }, + }, +}; + + +/* function called before the qemu main it will define our device */ +static void pci_example_register_types (void ) +{ +type_register_static (&pci_example_info); +} + +/* make qemu register our device at qemu-booting */ +type_init (pci_example_register_types) + + +Signature · 1 line
2.17.1