pci/pci_ivshmem: add pci ivshmem driver support

Add common ivshmem driver, provide the ivshmem bus mechisam, so
many ivshmem based driver can change to pci_ivshmem based

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
This commit is contained in:
Bowen Wang 2023-12-22 16:41:19 +08:00 committed by Xiang Xiao
parent ccb17f2bb4
commit 218210d26e
6 changed files with 737 additions and 0 deletions

View File

@ -31,6 +31,12 @@ config PCI_QEMU_EDU
---help---
Driver for QEMU EDU test device
config PCI_IVSHMEM
bool "PCI ivshmem driver"
default n
---help---
Enable Inter-VM share memory device driver.
config PCI_UIO_IVSHMEM
bool "Enable uio ivshmem driver support"
default n

View File

@ -29,6 +29,10 @@ ifeq ($(CONFIG_PCI_QEMU_EDU),y)
CSRCS += pci_qemu_edu.c
endif
ifeq ($(CONFIG_PCI_IVSHMEM),y)
CSRCS += pci_ivshmem.c
endif
ifeq ($(CONFIG_PCI_UIO_IVSHMEM),y)
CSRCS += pci_uio_ivshmem.c
endif

View File

@ -47,6 +47,14 @@ int pci_register_drivers(void)
{
int ret;
#ifdef CONFIG_PCI_IVSHMEM
ret = pci_ivshmem_register();
if (ret < 0)
{
pcierr("pci_ivshemem_register failed ret=%d\n", ret);
}
#endif
#ifdef CONFIG_PCI_UIO_IVSHMEM
ret = pci_register_uio_ivshmem_driver();
if (ret < 0)

View File

@ -31,6 +31,26 @@
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: pci_ivshmem_register
*
* Description:
* Register common pci ivshmem driver
*
****************************************************************************/
#ifdef CONFIG_PCI_IVSHMEM
int pci_ivshmem_register(void);
#endif
/****************************************************************************
* Name: pci_register_uio_ivshmem_driver
*
* Description:
* Register uio ivshmem device pci driver
*
****************************************************************************/
#ifdef CONFIG_PCI_UIO_IVSHMEM
int pci_register_uio_ivshmem_driver(void);
#endif

536
drivers/pci/pci_ivshmem.c Normal file
View File

@ -0,0 +1,536 @@
/****************************************************************************
* drivers/pci/pci_ivshmem.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/list.h>
#include <nuttx/mutex.h>
#include <nuttx/pci/pci.h>
#include <nuttx/pci/pci_ivshmem.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define IVSHMEM_REG_BAR 0
#define IVSHMEM_MSIX_BAR 1
#define IVSHMEM_SHMEM_BAR 2
#define IVSHMEM_REG_INT_MASK 0
#define IVSHMEM_REG_INT_STATUS 4
#define IVSHMEM_REG_IVPOSITION 8
#define IVSHMEM_REG_DOORBELL 12
#define IVSHMEM_INVALID_VMID UINT32_MAX
/****************************************************************************
* Private Types
****************************************************************************/
struct ivshmem_bus_s
{
mutex_t lock; /* Lock for the list */
struct list_node dev; /* Wait match ivshmem device list */
struct list_node drv; /* Ivshmem driver list */
};
struct ivshmem_device_s
{
FAR struct pci_device_s *dev;
FAR struct ivshmem_driver_s *drv;
struct list_node node;
int id;
FAR void *shmem;
size_t shmem_size;
FAR void *reg;
int irq;
uint32_t vmid;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int ivshmem_probe(FAR struct pci_device_s *dev);
static void ivshmem_remove(FAR struct pci_device_s *dev);
static int ivshmem_register_device(FAR struct ivshmem_device_s *dev);
static int ivshmem_unregister_device(FAR struct ivshmem_device_s *dev);
/****************************************************************************
* Private Data
****************************************************************************/
static int g_ivshmem_next_id = 0;
static struct ivshmem_bus_s g_ivshmem_bus =
{
NXMUTEX_INITIALIZER,
LIST_INITIAL_VALUE(g_ivshmem_bus.dev),
LIST_INITIAL_VALUE(g_ivshmem_bus.drv),
};
static const struct pci_device_id_s g_ivshmem_id_table[] =
{
{ PCI_DEVICE(0x1af4, 0x1110) },
{ 0, }
};
static struct pci_driver_s g_ivshmem_drv =
{
g_ivshmem_id_table, /* PCI id table */
ivshmem_probe, /* Probe function */
ivshmem_remove /* Remove function */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: ivshmem_register_device
****************************************************************************/
static int ivshmem_register_device(FAR struct ivshmem_device_s *dev)
{
FAR struct ivshmem_driver_s *drv;
int ret;
ret = nxmutex_lock(&g_ivshmem_bus.lock);
if (ret < 0)
{
return ret;
}
/* Add the device to the ivshmem_bus device list */
list_add_tail(&g_ivshmem_bus.dev, &dev->node);
/* Match the driver has registered in the ivshmem_bus */
list_for_every_entry(&g_ivshmem_bus.drv, drv, struct ivshmem_driver_s,
node)
{
if (drv->id == dev->id)
{
/* If found the driver in the driver list, call driver probe,
* if probe success, assign item->driver to indicate the device
* matched.
*/
dev->drv = drv;
if (drv->probe(dev) < 0)
{
dev->drv = NULL;
}
break;
}
}
nxmutex_unlock(&g_ivshmem_bus.lock);
return ret;
}
/****************************************************************************
* Name: ivshmem_unregister_device
****************************************************************************/
static int ivshmem_unregister_device(FAR struct ivshmem_device_s *dev)
{
int ret;
ret = nxmutex_lock(&g_ivshmem_bus.lock);
if (ret < 0)
{
return ret;
}
/* Remove the device from the device list */
list_delete(&dev->node);
nxmutex_unlock(&g_ivshmem_bus.lock);
/* Call driver remove and mark tmp->driver NULL to indicate
* the device unmatched
*/
dev->drv->remove(dev);
dev->drv = NULL;
return ret;
}
/****************************************************************************
* Name: ivshmem_probe
****************************************************************************/
static int ivshmem_probe(FAR struct pci_device_s *dev)
{
FAR struct ivshmem_device_s *ivdev;
int ret;
ivdev = kmm_zalloc(sizeof(*ivdev));
if (ivdev == NULL)
{
return -ENOMEM;
}
dev->priv = ivdev;
ivdev->dev = dev;
ivdev->id = g_ivshmem_next_id;
ret = pci_enable_device(dev);
if (ret < 0)
{
pciinfo("Enable device failed, ret=%d\n", ret);
goto err;
}
pci_set_master(dev);
ivdev->reg = pci_map_bar(dev, IVSHMEM_REG_BAR);
if (ivdev->reg == NULL)
{
pcierr("Device Not support Register Bar\n");
ret = -ENOTTY;
goto err_master;
}
ivdev->shmem = pci_map_bar(dev, IVSHMEM_SHMEM_BAR);
if (ivdev->shmem == NULL)
{
pcierr("Device Not support Share Memory Bar\n");
ret = -ENOTTY;
goto err_master;
}
ivdev->shmem_size = pci_resource_len(dev, IVSHMEM_SHMEM_BAR);
pciinfo("shmem addr=%p size=%zu reg=%p\n",
ivdev->shmem, ivdev->shmem_size, ivdev->reg);
if (pci_resource_flags(dev, IVSHMEM_MSIX_BAR) != 0)
{
pci_read_mmio_dword(dev,
(FAR char *)ivdev->reg + IVSHMEM_REG_IVPOSITION,
&ivdev->vmid);
ret = pci_alloc_irq(dev, &ivdev->irq, 1);
if (ret != 1)
{
pcierr("Failed to allocate irq %d\n", ret);
goto err_master;
}
ret = pci_connect_irq(dev, &ivdev->irq, 1);
if (ret < 0)
{
pcierr("Failed to connect irq %d\n", ret);
goto err_msi_alloc;
}
pciinfo("vmid=%" PRIu32 " irq=%d\n", ivdev->vmid, ivdev->irq);
}
else
{
ivdev->vmid = IVSHMEM_INVALID_VMID;
}
ret = ivshmem_register_device(ivdev);
if (ret < 0)
{
goto err_master;
}
g_ivshmem_next_id++;
return ret;
err_msi_alloc:
pci_release_irq(dev, &ivdev->irq, 1);
err_master:
pci_clear_master(dev);
pci_disable_device(dev);
err:
kmm_free(ivdev);
return ret;
}
/****************************************************************************
* Name: ivshmem_remove
****************************************************************************/
static void ivshmem_remove(FAR struct pci_device_s *dev)
{
FAR struct ivshmem_device_s *ivdev = dev->priv;
ivshmem_unregister_device(ivdev);
if (ivdev->reg != NULL)
{
pci_release_irq(dev, &ivdev->irq, 1);
}
pci_clear_master(ivdev->dev);
pci_disable_device(ivdev->dev);
kmm_free(ivdev);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ivshmem_get_driver
*
* Description:
* Get the ivshmem device corresponding driver.
*
****************************************************************************/
FAR struct ivshmem_driver_s *
ivshmem_get_driver(FAR struct ivshmem_device_s *dev)
{
return dev->drv;
}
/****************************************************************************
* Name: ivshmem_get_shmem
*
* Description:
* Get the ivshmem device share memory address and size.
*
****************************************************************************/
FAR void *ivshmem_get_shmem(FAR struct ivshmem_device_s *dev,
FAR size_t *size)
{
*size = dev->shmem_size;
return dev->shmem;
}
/****************************************************************************
* Name: ivshmem_attach_irq
*
* Description:
* Attach/detach the interrupt handler to the ivshmem interrupt
*
****************************************************************************/
int ivshmem_attach_irq(FAR struct ivshmem_device_s *dev, xcpt_t isr,
FAR void *arg)
{
if (dev->vmid == IVSHMEM_INVALID_VMID)
{
return -ENOTSUP;
}
return irq_attach(dev->irq, isr, arg);
}
/****************************************************************************
* Name: ivshmem_detach_irq
*
* Description:
* Detach the interrupt handler to the ivshmem interrupt
*
****************************************************************************/
int ivshmem_detach_irq(FAR struct ivshmem_device_s *dev)
{
if (dev->vmid == IVSHMEM_INVALID_VMID)
{
return -ENOTSUP;
}
return irq_detach(dev->irq);
}
/****************************************************************************
* Name: ivshmem_control_irq
*
* Description:
* Enable/Disable the ivshmem interrupt
*
****************************************************************************/
int ivshmem_control_irq(FAR struct ivshmem_device_s *dev, bool on)
{
if (dev->vmid == IVSHMEM_INVALID_VMID)
{
return -ENOTSUP;
}
if (on)
{
up_enable_irq(dev->irq);
}
else
{
up_disable_irq(dev->irq);
}
return OK;
}
/****************************************************************************
* Name: ivshmem_kick_peer
*
* Description:
* Send interrupt to peer
*
****************************************************************************/
int ivshmem_kick_peer(FAR struct ivshmem_device_s *dev)
{
uint32_t peer_id;
if (dev->vmid == IVSHMEM_INVALID_VMID)
{
return -ENOTSUP;
}
if (dev->vmid == 0)
{
peer_id = 1;
}
else
{
peer_id = 0;
}
/* We only use one vector, so vector is 0, peer_id must be 0 or 1
* bit 0..15: vector
* bit 16..31: peer ID
*/
pci_write_mmio_dword(dev->dev, dev->reg + IVSHMEM_REG_DOORBELL,
peer_id << 16);
return 0;
}
/****************************************************************************
* Name: ivshmem_register_driver
****************************************************************************/
int ivshmem_register_driver(FAR struct ivshmem_driver_s *drv)
{
FAR struct ivshmem_device_s *dev;
int ret;
DEBUGASSERT(drv != NULL && drv->probe != NULL &&
drv->remove != NULL);
ret = nxmutex_lock(&g_ivshmem_bus.lock);
if (ret < 0)
{
return ret;
}
/* Add the driver to the ivshmem_bus driver list */
list_add_tail(&g_ivshmem_bus.drv, &drv->node);
/* Match all the devices has registered in the ivshmem_bus */
list_for_every_entry(&g_ivshmem_bus.dev, dev,
struct ivshmem_device_s, node)
{
if (drv->id == dev->id)
{
/* If found the device in the device list, call driver probe,
* if probe success, assign item->driver to indicate the device
* matched.
*/
dev->drv = drv;
if (drv->probe(dev) < 0)
{
dev->drv = NULL;
}
}
}
nxmutex_unlock(&g_ivshmem_bus.lock);
return ret;
}
/****************************************************************************
* Name: ivshmem_unregister_driver
****************************************************************************/
int ivshmem_unregister_driver(FAR struct ivshmem_driver_s *drv)
{
FAR struct ivshmem_device_s *dev;
int ret;
DEBUGASSERT(drv != NULL);
ret = nxmutex_lock(&g_ivshmem_bus.lock);
if (ret < 0)
{
return ret;
}
/* Find all the devices matched with driver in device list */
list_for_every_entry(&g_ivshmem_bus.dev, dev,
struct ivshmem_device_s, node)
{
if (dev->drv == drv)
{
/* 1. Call driver remove function;
* 2. Mark dev->drv NULL to indicate the device unmatched;
*/
drv->remove(dev);
dev->drv = NULL;
}
}
/* Remove the driver from the driver list */
list_delete(&drv->node);
nxmutex_unlock(&g_ivshmem_bus.lock);
return ret;
}
/****************************************************************************
* Name: pci_ivshmem_register
****************************************************************************/
int pci_ivshmem_register(void)
{
return pci_register_driver(&g_ivshmem_drv);
}

View File

@ -0,0 +1,163 @@
/****************************************************************************
* include/nuttx/pci/pci_ivshmem.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_PCI_PCI_IVSHMEM_H
#define __INCLUDE_NUTTX_PCI_PCI_IVSHMEM_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_PCI_IVSHMEM
#include <nuttx/list.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
struct ivshmem_device_s;
struct ivshmem_driver_s
{
int id;
CODE int (*probe)(FAR struct ivshmem_device_s *dev);
CODE void (*remove)(FAR struct ivshmem_device_s *dev);
struct list_node node;
};
/****************************************************************************
* Public Functions Prototypes
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: ivshmem_get_driver
*
* Description:
* Get the ivshmem device corresponding driver.
*
****************************************************************************/
FAR struct ivshmem_driver_s *
ivshmem_get_driver(FAR struct ivshmem_device_s *dev);
/****************************************************************************
* Name: ivshmem_get_shmem
*
* Description:
* Get the ivshmem device share memory address and size.
*
****************************************************************************/
FAR void *ivshmem_get_shmem(FAR struct ivshmem_device_s *dev,
FAR size_t *size);
/****************************************************************************
* Name: ivshmem_attach_irq
*
* Description:
* Attach the interrupt handler to the ivshmem interrupt
*
****************************************************************************/
int ivshmem_attach_irq(FAR struct ivshmem_device_s *dev, xcpt_t isr,
FAR void *arg);
/****************************************************************************
* Name: ivshmem_detach_irq
*
* Description:
* Detach the interrupt handler to the ivshmem interrupt
*
****************************************************************************/
int ivshmem_detach_irq(FAR struct ivshmem_device_s *dev);
/****************************************************************************
* Name: ivshmem_control_irq
*
* Description:
* Enable/Disable the ivshmem interrupt
*
****************************************************************************/
int ivshmem_control_irq(FAR struct ivshmem_device_s *dev, bool on);
/****************************************************************************
* Name: ivshmem_kick_peer
*
* Description:
* Send interrupt to peer
*
****************************************************************************/
int ivshmem_kick_peer(FAR struct ivshmem_device_s *dev);
/****************************************************************************
* Name: ivshmem_register_driver
*
* Description:
* Register ivshmem driver to ivshmem bus
*
****************************************************************************/
int ivshmem_register_driver(FAR struct ivshmem_driver_s *drv);
/****************************************************************************
* Name: ivshmem_unregister_driver
*
* Description:
* Un-register ivshmem driver from ivshmem bus
*
****************************************************************************/
int ivshmem_unregister_driver(FAR struct ivshmem_driver_s *drv);
/****************************************************************************
* Name: pci_ivshemem_register
*
* Description:
* Register the pci ivshmem driver to the pci bus.
*
****************************************************************************/
int pci_ivshemem_register(void);
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* CONFIG_PCI_IVSHMEM */
#endif /* __INCLUDE_NUTTX_PCI_PCI_IVSHMEM_H */