virtio-mmio/pci: add alloc_buf/free_buf for mmio and pci transport layer

And remove the virtio_alloc/free_buf implementation in virtio.c, because
these two apis has been moved to the OpenAMP

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
This commit is contained in:
Bowen Wang 2023-10-31 21:15:09 +08:00 committed by Xiang Xiao
parent 0b1f45d600
commit 89c49c53a7
4 changed files with 75 additions and 57 deletions

View File

@ -196,6 +196,10 @@ static uint32_t virtio_mmio_get_queue_len(FAR struct metal_io_region *io,
int idx); int idx);
static int virtio_mmio_config_virtqueue(FAR struct metal_io_region *io, static int virtio_mmio_config_virtqueue(FAR struct metal_io_region *io,
FAR struct virtqueue *vq); FAR struct virtqueue *vq);
static FAR void *virtio_mmio_alloc_buf(FAR struct virtio_device *vdev,
size_t size, size_t align);
static void virtio_mmio_free_buf(FAR struct virtio_device *vdev,
FAR void *buf);
static int virtio_mmio_init_device(FAR struct virtio_mmio_device_s *vmdev, static int virtio_mmio_init_device(FAR struct virtio_mmio_device_s *vmdev,
FAR void *regs, int irq); FAR void *regs, int irq);
@ -252,6 +256,12 @@ static const struct virtio_dispatch g_virtio_mmio_dispatch =
virtio_mmio_notify, /* notify */ virtio_mmio_notify, /* notify */
}; };
static const struct virtio_memory_ops g_virtio_mmio_mmops =
{
virtio_mmio_alloc_buf, /* Alloc */
virtio_mmio_free_buf, /* Free */
};
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
@ -749,6 +759,26 @@ static int virtio_mmio_interrupt(int irq, FAR void *context, FAR void *arg)
return OK; return OK;
} }
/****************************************************************************
* Name: virtio_mmio_alloc_buf
****************************************************************************/
static FAR void *virtio_mmio_alloc_buf(FAR struct virtio_device *vdev,
size_t size, size_t align)
{
return kmm_memalign(align, size);
}
/****************************************************************************
* Name: virtio_mmio_free_buf
****************************************************************************/
static void virtio_mmio_free_buf(FAR struct virtio_device *vdev,
FAR void *buf)
{
kmm_free(buf);
}
/**************************************************************************** /****************************************************************************
* Name: virtio_mmio_init_device * Name: virtio_mmio_init_device
****************************************************************************/ ****************************************************************************/
@ -778,6 +808,7 @@ static int virtio_mmio_init_device(FAR struct virtio_mmio_device_s *vmdev,
vdev->role = VIRTIO_DEV_DRIVER; vdev->role = VIRTIO_DEV_DRIVER;
vdev->func = &g_virtio_mmio_dispatch; vdev->func = &g_virtio_mmio_dispatch;
vdev->mmops = &g_virtio_mmio_mmops;
magic = metal_io_read32(&vmdev->cfg_io, VIRTIO_MMIO_MAGIC_VALUE); magic = metal_io_read32(&vmdev->cfg_io, VIRTIO_MMIO_MAGIC_VALUE);
if (magic != VIRTIO_MMIO_MAGIC_VALUE_STRING) if (magic != VIRTIO_MMIO_MAGIC_VALUE_STRING)

View File

@ -45,6 +45,11 @@
* Private Function Prototypes * Private Function Prototypes
****************************************************************************/ ****************************************************************************/
static FAR void *virtio_pci_alloc_buf(FAR struct virtio_device *vdev,
size_t size, size_t align);
static void virtio_pci_free_buf(FAR struct virtio_device *vdev,
FAR void *buf);
static int virtio_pci_probe(FAR struct pci_device_s *dev); static int virtio_pci_probe(FAR struct pci_device_s *dev);
static void virtio_pci_remove(FAR struct pci_device_s *dev); static void virtio_pci_remove(FAR struct pci_device_s *dev);
@ -69,6 +74,12 @@ static struct pci_driver_s g_virtio_pci_drv =
virtio_pci_remove virtio_pci_remove
}; };
static const struct virtio_memory_ops g_virtio_pci_mmops =
{
virtio_pci_alloc_buf, /* Alloc */
virtio_pci_free_buf, /* Free */
};
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
@ -135,6 +146,26 @@ static void virtio_pci_wdog(wdparm_t arg)
} }
#endif #endif
/****************************************************************************
* Name: virtio_pci_alloc_buf
****************************************************************************/
static FAR void *virtio_pci_alloc_buf(FAR struct virtio_device *vdev,
size_t size, size_t align)
{
return kmm_memalign(align, size);
}
/****************************************************************************
* Name: virtio_pci_free_buf
****************************************************************************/
static void virtio_pci_free_buf(FAR struct virtio_device *vdev,
FAR void *buf)
{
return kmm_free(buf);
}
/**************************************************************************** /****************************************************************************
* Name: virtio_pci_probe * Name: virtio_pci_probe
****************************************************************************/ ****************************************************************************/
@ -163,6 +194,7 @@ static int virtio_pci_probe(FAR struct pci_device_s *dev)
dev->priv = vpdev; dev->priv = vpdev;
vpdev->dev = dev; vpdev->dev = dev;
vdev = &vpdev->vdev; vdev = &vpdev->vdev;
vdev->mmops = &g_virtio_pci_mmops;
/* Virtio device initialize */ /* Virtio device initialize */

View File

@ -72,48 +72,6 @@ static struct virtio_bus_s g_virtio_bus =
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: virtio_alloc_buf
****************************************************************************/
FAR void *virtio_alloc_buf(FAR struct virtio_device *vdev,
size_t size, size_t align)
{
if (align == 0)
{
return kmm_malloc(size);
}
else
{
return kmm_memalign(align, size);
}
}
/****************************************************************************
* Name: virtio_zalloc_buf
****************************************************************************/
FAR void *virtio_zalloc_buf(FAR struct virtio_device *vdev,
size_t size, size_t align)
{
FAR void *ptr = virtio_alloc_buf(vdev, size, align);
if (ptr != NULL)
{
memset(ptr, 0, size);
}
return ptr;
}
/****************************************************************************
* Name: virtio_mmio_free_buf
****************************************************************************/
void virtio_free_buf(FAR struct virtio_device *vdev, FAR void *buf)
{
kmm_free(buf);
}
/**************************************************************************** /****************************************************************************
* Name: virtio_register_drivers * Name: virtio_register_drivers
****************************************************************************/ ****************************************************************************/

View File

@ -208,6 +208,18 @@ extern "C"
#define EXTERN extern #define EXTERN extern
#endif #endif
static inline_function FAR void *
virtio_zalloc_buf(FAR struct virtio_device *vdev, size_t size, size_t align)
{
FAR void *buf = virtio_alloc_buf(vdev, size, align);
if (buf != NULL)
{
memset(buf, 0, size);
}
return buf;
}
/* Driver and device register/unregister function */ /* Driver and device register/unregister function */
int virtio_register_driver(FAR struct virtio_driver *driver); int virtio_register_driver(FAR struct virtio_driver *driver);
@ -216,21 +228,6 @@ int virtio_register_device(FAR struct virtio_device *device);
int virtio_unregister_driver(FAR struct virtio_driver *driver); int virtio_unregister_driver(FAR struct virtio_driver *driver);
int virtio_unregister_device(FAR struct virtio_device *device); int virtio_unregister_device(FAR struct virtio_device *device);
/* Virtio alloc/free buffer API
* NOTE:
* For now, these three apis are implemented in NuttX, and direclty mapping
* to kmm_memalgin/kmm_free, so it's only compatible with virtio mmio
* transport for now. After the virtio remoteproc transport layer completed,
* these three apis should be moved to OpenAmp, and different transport layer
* provide different implementation.
*/
FAR void *virtio_alloc_buf(FAR struct virtio_device *vdev,
size_t size, size_t align);
FAR void *virtio_zalloc_buf(FAR struct virtio_device *vdev,
size_t size, size_t align);
void virtio_free_buf(FAR struct virtio_device *vdev, FAR void *buf);
/* Virtio driver initailied function, called in NuttX driver_intialize() */ /* Virtio driver initailied function, called in NuttX driver_intialize() */
void virtio_register_drivers(void); void virtio_register_drivers(void);