This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 13e37be852553ff040a03d55a0124973fa02f416 Author: Bowen Wang <[email protected]> AuthorDate: Fri Oct 23 21:16:53 2026 +0800 drivers/ivshmem: split the ivshmem bus out of pci_ivshmem The device/driver matching and the irq/doorbell helpers in pci_ivshmem.c do not depend on PCI, but they could only be built with CONFIG_PCI_IVSHMEM, so a machine without a PCI bus could not use rptun_ivshmem or the other ivshmem drivers. Move them to drivers/ivshmem/ivshmem.c under a new CONFIG_IVSHMEM, selected by PCI_IVSHMEM. pci_ivshmem.c keeps only the PCI probe and registers the device through ivshmem_register_device(); the doorbell write goes through ivshmem_ops_s. The public API and the drivers on top of it are unchanged. Signed-off-by: Bowen Wang <[email protected]> --- drivers/CMakeLists.txt | 4 +- drivers/Kconfig | 1 + drivers/Makefile | 1 + drivers/{ => ivshmem}/CMakeLists.txt | 16 +- drivers/ivshmem/Kconfig | 12 + drivers/ivshmem/Make.defs | 30 ++ drivers/{pci/pci_ivshmem.c => ivshmem/ivshmem.c} | 225 +------------ drivers/pci/Kconfig | 1 + drivers/pci/pci_ivshmem.c | 408 +++-------------------- include/nuttx/{pci/pci_ivshmem.h => ivshmem.h} | 78 ++++- include/nuttx/pci/pci_ivshmem.h | 115 +------ 11 files changed, 184 insertions(+), 707 deletions(-) diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 22fecc00077..7434b844bbe 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -22,8 +22,8 @@ nuttx_add_kernel_library(drivers) -# Subdirectories that provide their own CMakeLists.txt (for example dma-buf) are -# included automatically. +# Subdirectories that provide their own CMakeLists.txt (for example dma-buf and +# ivshmem) are included automatically. nuttx_add_subdirectory() # The TEE, SENT and Trace32 drivers are discovered through their own diff --git a/drivers/Kconfig b/drivers/Kconfig index 17060df09d9..0c75b51c044 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -86,6 +86,7 @@ source "drivers/dma-buf/Kconfig" source "drivers/devicetree/Kconfig" source "drivers/reset/Kconfig" source "drivers/pci/Kconfig" +source "drivers/ivshmem/Kconfig" source "drivers/coresight/Kconfig" source "drivers/aie/Kconfig" source "drivers/tee/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index 6e36faab152..7340c715987 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -86,6 +86,7 @@ include segger/Make.defs include usrsock/Make.defs include reset/Make.defs include pci/Make.defs +include ivshmem/Make.defs include coresight/Make.defs include aie/Make.defs include tee/Make.defs diff --git a/drivers/CMakeLists.txt b/drivers/ivshmem/CMakeLists.txt similarity index 65% copy from drivers/CMakeLists.txt copy to drivers/ivshmem/CMakeLists.txt index 22fecc00077..2ed330c558f 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/ivshmem/CMakeLists.txt @@ -1,5 +1,5 @@ # ############################################################################## -# drivers/CMakeLists.txt +# drivers/ivshmem/CMakeLists.txt # # SPDX-License-Identifier: Apache-2.0 # @@ -18,15 +18,7 @@ # License for the specific language governing permissions and limitations under # the License. # -# ############################################################################## - -nuttx_add_kernel_library(drivers) - -# Subdirectories that provide their own CMakeLists.txt (for example dma-buf) are -# included automatically. -nuttx_add_subdirectory() -# The TEE, SENT and Trace32 drivers are discovered through their own -# CMakeLists.txt. -target_sources(drivers PRIVATE drivers_initialize.c) -target_include_directories(drivers PRIVATE ${CMAKE_CURRENT_LIST_DIR}) +if(CONFIG_IVSHMEM) + target_sources(drivers PRIVATE ivshmem.c) +endif() diff --git a/drivers/ivshmem/Kconfig b/drivers/ivshmem/Kconfig new file mode 100644 index 00000000000..280197042ba --- /dev/null +++ b/drivers/ivshmem/Kconfig @@ -0,0 +1,12 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config IVSHMEM + bool + default n + ---help--- + Inter-VM shared memory bus. Matches ivshmem devices registered by + a backend (e.g. PCI) with the drivers that use them. Selected by + the backends, not chosen directly. diff --git a/drivers/ivshmem/Make.defs b/drivers/ivshmem/Make.defs new file mode 100644 index 00000000000..7166e941608 --- /dev/null +++ b/drivers/ivshmem/Make.defs @@ -0,0 +1,30 @@ +############################################################################ +# drivers/ivshmem/Make.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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. +# +############################################################################ + +ifeq ($(CONFIG_IVSHMEM),y) + +CSRCS += ivshmem.c + +DEPPATH += --dep-path ivshmem +VPATH += :ivshmem + +endif # CONFIG_IVSHMEM diff --git a/drivers/pci/pci_ivshmem.c b/drivers/ivshmem/ivshmem.c similarity index 62% copy from drivers/pci/pci_ivshmem.c copy to drivers/ivshmem/ivshmem.c index 8004516017d..2c14eabbc24 100644 --- a/drivers/pci/pci_ivshmem.c +++ b/drivers/ivshmem/ivshmem.c @@ -1,5 +1,5 @@ /**************************************************************************** - * drivers/pci/pci_ivshmem.c + * drivers/ivshmem/ivshmem.c * * SPDX-License-Identifier: Apache-2.0 * @@ -26,31 +26,14 @@ #include <nuttx/config.h> -#include <stdint.h> -#include <stdio.h> #include <errno.h> -#include <nuttx/debug.h> +#include <debug.h> #include <nuttx/arch.h> -#include <nuttx/kmalloc.h> +#include <nuttx/irq.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 +#include <nuttx/percpu.h> +#include <nuttx/ivshmem.h> /**************************************************************************** * Private Types @@ -63,33 +46,6 @@ struct ivshmem_bus_s dq_queue_t drv; /* Ivshmem driver queue */ }; -struct ivshmem_device_s -{ - FAR struct pci_device_s *dev; - - FAR struct ivshmem_driver_s *drv; - dq_entry_t 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 ****************************************************************************/ @@ -103,38 +59,32 @@ static DEFINE_PER_CPU_BMP(struct ivshmem_bus_s, g_ivshmem_bus) = }; #define g_ivshmem_bus this_cpu_var_bmp(g_ivshmem_bus) -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 + * Public Functions ****************************************************************************/ /**************************************************************************** * Name: ivshmem_register_device ****************************************************************************/ -static int ivshmem_register_device(FAR struct ivshmem_device_s *dev) +int ivshmem_register_device(FAR struct ivshmem_device_s *dev, + ivshmem_kick_peer_t kick_peer) { FAR struct ivshmem_driver_s *drv; int ret; + DEBUGASSERT(dev != NULL); + ret = nxmutex_lock(&g_ivshmem_bus.lock); if (ret < 0) { return ret; } + dev->kick_peer = kick_peer; + + dev->id = g_ivshmem_next_id++; + /* Add the device to the ivshmem_bus device queue */ dq_addlast(&dev->node, &g_ivshmem_bus.dev); @@ -168,7 +118,7 @@ static int ivshmem_register_device(FAR struct ivshmem_device_s *dev) * Name: ivshmem_unregister_device ****************************************************************************/ -static int ivshmem_unregister_device(FAR struct ivshmem_device_s *dev) +int ivshmem_unregister_device(FAR struct ivshmem_device_s *dev) { int ret; @@ -196,133 +146,6 @@ static int ivshmem_unregister_device(FAR struct ivshmem_device_s *dev) 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); - - if (ivdev->vmid != 0 && ivdev->vmid != 1) - { - pciwarn("Vmid=%"PRIu32" not 0/1, use polling mode\n", ivdev->vmid); - ivdev->vmid = IVSHMEM_INVALID_VMID; - goto out; - } - - ret = pci_alloc_irq(dev, &ivdev->irq, 1); - if (ret != 1) - { - pciwarn("Failed to allocate irq %d, use polling mode\n", ret); - ivdev->vmid = IVSHMEM_INVALID_VMID; - goto out; - } - - ret = pci_connect_irq(dev, &ivdev->irq, 1); - if (ret < 0) - { - pciwarn("Failed to connect irq %d, use polling mode\n", ret); - pci_release_irq(dev, &ivdev->irq, 1); - ivdev->vmid = IVSHMEM_INVALID_VMID; - goto out; - } - - pciinfo("vmid=%" PRIu32 " irq=%d\n", ivdev->vmid, ivdev->irq); - } - else - { - ivdev->vmid = IVSHMEM_INVALID_VMID; - } - -out: - ret = ivshmem_register_device(ivdev); - if (ret < 0) - { - goto err_master; - } - - g_ivshmem_next_id++; - return ret; - -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->vmid != IVSHMEM_INVALID_VMID) - { - 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 * @@ -455,14 +278,9 @@ int ivshmem_kick_peer(FAR struct ivshmem_device_s *dev) 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 - */ + DEBUGASSERT(dev->kick_peer != NULL); - pci_write_mmio_dword(dev->dev, dev->reg + IVSHMEM_REG_DOORBELL, - peer_id << 16); - return 0; + return dev->kick_peer(dev, peer_id); } /**************************************************************************** @@ -549,12 +367,3 @@ int ivshmem_unregister_driver(FAR struct ivshmem_driver_s *drv) nxmutex_unlock(&g_ivshmem_bus.lock); return ret; } - -/**************************************************************************** - * Name: pci_ivshmem_register - ****************************************************************************/ - -int pci_ivshmem_register(void) -{ - return pci_register_driver(&g_ivshmem_drv); -} diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig index 12b2a7ef625..18f5abbacda 100644 --- a/drivers/pci/Kconfig +++ b/drivers/pci/Kconfig @@ -70,6 +70,7 @@ config PCI_QEMU_EDU config PCI_IVSHMEM bool "PCI ivshmem driver" default n + select IVSHMEM ---help--- Enable Inter-VM share memory device driver. diff --git a/drivers/pci/pci_ivshmem.c b/drivers/pci/pci_ivshmem.c index 8004516017d..4d992377544 100644 --- a/drivers/pci/pci_ivshmem.c +++ b/drivers/pci/pci_ivshmem.c @@ -31,9 +31,8 @@ #include <errno.h> #include <nuttx/debug.h> -#include <nuttx/arch.h> #include <nuttx/kmalloc.h> -#include <nuttx/mutex.h> +#include <nuttx/nuttx.h> #include <nuttx/pci/pci.h> #include <nuttx/pci/pci_ivshmem.h> @@ -45,75 +44,40 @@ #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 queue */ - dq_queue_t dev; /* Wait match ivshmem device queue */ - dq_queue_t drv; /* Ivshmem driver queue */ -}; - -struct ivshmem_device_s +struct pci_ivshmem_s { + struct ivshmem_device_s ivdev; FAR struct pci_device_s *dev; - - FAR struct ivshmem_driver_s *drv; - dq_entry_t 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); +static int pci_ivshmem_probe(FAR struct pci_device_s *dev); +static void pci_ivshmem_remove(FAR struct pci_device_s *dev); +static int pci_ivshmem_kick_peer(FAR struct ivshmem_device_s *ivdev, + uint32_t peer_id); /**************************************************************************** * Private Data ****************************************************************************/ -static DEFINE_PER_CPU_BSS_BMP(int, g_ivshmem_next_id); -#define g_ivshmem_next_id this_cpu_var_bmp(g_ivshmem_next_id) - -static DEFINE_PER_CPU_BMP(struct ivshmem_bus_s, g_ivshmem_bus) = -{ - NXMUTEX_INITIALIZER, -}; -#define g_ivshmem_bus this_cpu_var_bmp(g_ivshmem_bus) - -static const struct pci_device_id_s g_ivshmem_id_table[] = +static const struct pci_device_id_s g_pci_ivshmem_id_table[] = { { PCI_DEVICE(0x1af4, 0x1110) }, { 0, } }; -static struct pci_driver_s g_ivshmem_drv = +static struct pci_driver_s g_pci_ivshmem_drv = { - g_ivshmem_id_table, /* PCI id table */ - ivshmem_probe, /* Probe function */ - ivshmem_remove /* Remove function */ + g_pci_ivshmem_id_table, /* PCI id table */ + pci_ivshmem_probe, /* Probe function */ + pci_ivshmem_remove /* Remove function */ }; /**************************************************************************** @@ -121,99 +85,45 @@ static struct pci_driver_s g_ivshmem_drv = ****************************************************************************/ /**************************************************************************** - * Name: ivshmem_register_device + * Name: pci_ivshmem_kick_peer ****************************************************************************/ -static int ivshmem_register_device(FAR struct ivshmem_device_s *dev) +static int pci_ivshmem_kick_peer(FAR struct ivshmem_device_s *ivdev, + uint32_t peer_id) { - 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 queue */ - - dq_addlast(&dev->node, &g_ivshmem_bus.dev); + FAR struct pci_ivshmem_s *priv = + container_of(ivdev, struct pci_ivshmem_s, ivdev); - /* Match the driver has registered in the ivshmem_bus */ - - dq_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 queue, 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 queue */ - - dq_rem(&dev->node, &g_ivshmem_bus.dev); - - nxmutex_unlock(&g_ivshmem_bus.lock); - - /* Call driver remove and mark tmp->driver NULL to indicate - * the device unmatched + /* 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 */ - if (dev->drv) - { - dev->drv->remove(dev); - } - - return ret; + pci_write_mmio_dword(priv->dev, + (FAR char *)ivdev->reg + IVSHMEM_REG_DOORBELL, + peer_id << 16); + return 0; } /**************************************************************************** - * Name: ivshmem_probe + * Name: pci_ivshmem_probe ****************************************************************************/ -static int ivshmem_probe(FAR struct pci_device_s *dev) +static int pci_ivshmem_probe(FAR struct pci_device_s *dev) { + FAR struct pci_ivshmem_s *priv; FAR struct ivshmem_device_s *ivdev; int ret; - ivdev = kmm_zalloc(sizeof(*ivdev)); - if (ivdev == NULL) + priv = kmm_zalloc(sizeof(*priv)); + if (priv == NULL) { return -ENOMEM; } - dev->priv = ivdev; - ivdev->dev = dev; - ivdev->id = g_ivshmem_next_id; + dev->priv = priv; + priv->dev = dev; + ivdev = &priv->ivdev; ret = pci_enable_device(dev); if (ret < 0) @@ -283,30 +193,37 @@ static int ivshmem_probe(FAR struct pci_device_s *dev) } out: - ret = ivshmem_register_device(ivdev); + ret = ivshmem_register_device(ivdev, pci_ivshmem_kick_peer); if (ret < 0) { goto err_master; } - g_ivshmem_next_id++; +#ifdef CONFIG_PCI_RTT_IVSHMEM + if (CONFIG_PCI_RTT_IVSHMEM_IDX == ivdev->id) + { + g_segger_offset = (uintptr_t)ivdev->shmem - (uintptr_t)&_SEGGER_RTT; + } +#endif + return ret; err_master: pci_clear_master(dev); pci_disable_device(dev); err: - kmm_free(ivdev); + kmm_free(priv); return ret; } /**************************************************************************** - * Name: ivshmem_remove + * Name: pci_ivshmem_remove ****************************************************************************/ -static void ivshmem_remove(FAR struct pci_device_s *dev) +static void pci_ivshmem_remove(FAR struct pci_device_s *dev) { - FAR struct ivshmem_device_s *ivdev = dev->priv; + FAR struct pci_ivshmem_s *priv = dev->priv; + FAR struct ivshmem_device_s *ivdev = &priv->ivdev; ivshmem_unregister_device(ivdev); if (ivdev->vmid != IVSHMEM_INVALID_VMID) @@ -314,247 +231,20 @@ static void ivshmem_remove(FAR struct pci_device_s *dev) pci_release_irq(dev, &ivdev->irq, 1); } - pci_clear_master(ivdev->dev); - pci_disable_device(ivdev->dev); - kmm_free(ivdev); + pci_clear_master(priv->dev); + pci_disable_device(priv->dev); + kmm_free(priv); } /**************************************************************************** * 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_support_irq - * - * Description: - * Judge if support ivshmem interrupt - * - ****************************************************************************/ - -bool ivshmem_support_irq(FAR struct ivshmem_device_s *dev) -{ - return dev->vmid != IVSHMEM_INVALID_VMID; -} - -/**************************************************************************** - * 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 queue */ - - dq_addlast(&drv->node, &g_ivshmem_bus.drv); - - /* Match all the devices has registered in the ivshmem_bus */ - - dq_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 queue, 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 queue */ - - dq_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 queue */ - - dq_rem(&drv->node, &g_ivshmem_bus.drv); - - nxmutex_unlock(&g_ivshmem_bus.lock); - return ret; -} - /**************************************************************************** * Name: pci_ivshmem_register ****************************************************************************/ int pci_ivshmem_register(void) { - return pci_register_driver(&g_ivshmem_drv); + return pci_register_driver(&g_pci_ivshmem_drv); } diff --git a/include/nuttx/pci/pci_ivshmem.h b/include/nuttx/ivshmem.h similarity index 72% copy from include/nuttx/pci/pci_ivshmem.h copy to include/nuttx/ivshmem.h index 3dc167b5730..dc6e6fe1f29 100644 --- a/include/nuttx/pci/pci_ivshmem.h +++ b/include/nuttx/ivshmem.h @@ -1,5 +1,5 @@ /**************************************************************************** - * include/nuttx/pci/pci_ivshmem.h + * include/nuttx/ivshmem.h * * SPDX-License-Identifier: Apache-2.0 * @@ -20,17 +20,20 @@ * ****************************************************************************/ -#ifndef __INCLUDE_NUTTX_PCI_PCI_IVSHMEM_H -#define __INCLUDE_NUTTX_PCI_PCI_IVSHMEM_H +#ifndef __INCLUDE_NUTTX_IVSHMEM_H +#define __INCLUDE_NUTTX_IVSHMEM_H /**************************************************************************** * Included Files ****************************************************************************/ #include <nuttx/config.h> -#include <stdbool.h> -#ifdef CONFIG_PCI_IVSHMEM +#ifdef CONFIG_IVSHMEM + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> #include <nuttx/irq.h> #include <nuttx/queue.h> @@ -39,11 +42,26 @@ * Pre-processor Definitions ****************************************************************************/ +/* Register layout from QEMU docs/specs/ivshmem-spec.rst */ + +#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 + /**************************************************************************** * Public Types ****************************************************************************/ struct ivshmem_device_s; + +/* Notifies the peer, or NULL for a device that cannot */ + +typedef CODE int (*ivshmem_kick_peer_t) + (FAR struct ivshmem_device_s *dev, uint32_t peer_id); + struct ivshmem_driver_s { int id; @@ -52,11 +70,32 @@ struct ivshmem_driver_s dq_entry_t node; }; +/* Filled in by a backend and embedded in its own device structure. The + * id is assigned by ivshmem_register_device(). + */ + +struct ivshmem_device_s +{ + ivshmem_kick_peer_t kick_peer; + + FAR struct ivshmem_driver_s *drv; + dq_entry_t node; + + int id; + + FAR void *shmem; + size_t shmem_size; + + FAR void *reg; + int irq; + uint32_t vmid; +}; + /**************************************************************************** - * Public Functions Prototypes + * Public Function Prototypes ****************************************************************************/ -#if defined(__cplusplus) +#ifdef __cplusplus extern "C" { #endif @@ -155,18 +194,31 @@ int ivshmem_register_driver(FAR struct ivshmem_driver_s *drv); int ivshmem_unregister_driver(FAR struct ivshmem_driver_s *drv); /**************************************************************************** - * Name: pci_ivshemem_register + * Name: ivshmem_register_device + * + * Description: + * Called by a backend to add a device to the ivshmem bus. Assigns the + * device id and probes the matching driver, if one is registered. + * kick_peer notifies the peer, or is NULL when the device has none. + * + ****************************************************************************/ + +int ivshmem_register_device(FAR struct ivshmem_device_s *dev, + ivshmem_kick_peer_t kick_peer); + +/**************************************************************************** + * Name: ivshmem_unregister_device * * Description: - * Register the pci ivshmem driver to the pci bus. + * Called by a backend to remove a device from the ivshmem bus. * ****************************************************************************/ -int pci_ivshemem_register(void); +int ivshmem_unregister_device(FAR struct ivshmem_device_s *dev); -#if defined(__cplusplus) +#ifdef __cplusplus } #endif -#endif /* CONFIG_PCI_IVSHMEM */ -#endif /* __INCLUDE_NUTTX_PCI_PCI_IVSHMEM_H */ +#endif /* CONFIG_IVSHMEM */ +#endif /* __INCLUDE_NUTTX_IVSHMEM_H */ diff --git a/include/nuttx/pci/pci_ivshmem.h b/include/nuttx/pci/pci_ivshmem.h index 3dc167b5730..1d5f94d2343 100644 --- a/include/nuttx/pci/pci_ivshmem.h +++ b/include/nuttx/pci/pci_ivshmem.h @@ -32,28 +32,10 @@ #ifdef CONFIG_PCI_IVSHMEM -#include <nuttx/irq.h> -#include <nuttx/queue.h> +#include <nuttx/ivshmem.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); - dq_entry_t node; -}; - -/**************************************************************************** - * Public Functions Prototypes + * Public Function Prototypes ****************************************************************************/ #if defined(__cplusplus) @@ -61,99 +43,6 @@ extern "C" { #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_support_irq - * - * Description: - * judge if support ivshmem interrupt - * - ****************************************************************************/ - -bool ivshmem_support_irq(FAR struct ivshmem_device_s *dev); - -/**************************************************************************** - * 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 *
