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 b367be1462138a97f8c169ac79240b746ba4e043 Author: Bowen Wang <[email protected]> AuthorDate: Tue Oct 27 21:50:14 2026 +0800 drivers/ivshmem: add the QEMU ivshmem-flat backend QEMU ivshmem-flat is the ivshmem register block and shared memory as plain MMIO regions plus one IRQ, for the M-profile machines without a PCI bus. Add a backend that registers it on the ivshmem bus. A device without an IRQ has no doorbell and its drivers poll, the same as ivshmem-plain without MSI-X. Signed-off-by: Bowen Wang <[email protected]> --- drivers/ivshmem/CMakeLists.txt | 4 ++ drivers/ivshmem/Kconfig | 11 ++++ drivers/ivshmem/Make.defs | 4 ++ drivers/ivshmem/ivshmem_flat.c | 118 +++++++++++++++++++++++++++++++++++++++++ include/nuttx/ivshmem_flat.h | 80 ++++++++++++++++++++++++++++ 5 files changed, 217 insertions(+) diff --git a/drivers/ivshmem/CMakeLists.txt b/drivers/ivshmem/CMakeLists.txt index 76ab056b2e4..e8e009fb1e5 100644 --- a/drivers/ivshmem/CMakeLists.txt +++ b/drivers/ivshmem/CMakeLists.txt @@ -30,5 +30,9 @@ if(CONFIG_IVSHMEM) list(APPEND SRCS ivshmem_uio.c) endif() + if(CONFIG_IVSHMEM_FLAT) + list(APPEND SRCS ivshmem_flat.c) + endif() + target_sources(drivers PRIVATE ${SRCS}) endif() diff --git a/drivers/ivshmem/Kconfig b/drivers/ivshmem/Kconfig index 1989cc54eaa..5e9811bab23 100644 --- a/drivers/ivshmem/Kconfig +++ b/drivers/ivshmem/Kconfig @@ -45,3 +45,14 @@ config UIO_IVSHMEM_NPOLLWAITERS int "Uio ivshmem poll waiter number" default 2 depends on UIO_IVSHMEM + +config IVSHMEM_FLAT + bool "QEMU ivshmem-flat driver" + default n + select IVSHMEM + ---help--- + Driver for the QEMU ivshmem-flat device: the ivshmem register + block and shared memory as plain MMIO regions plus one irq, for + machines without a PCI bus. The board registers each device with + ivshmem_flat_register(), or from the device tree with + fdt_ivshmem_flat_devices_register(). diff --git a/drivers/ivshmem/Make.defs b/drivers/ivshmem/Make.defs index 212359db14f..8f92732bb71 100644 --- a/drivers/ivshmem/Make.defs +++ b/drivers/ivshmem/Make.defs @@ -32,6 +32,10 @@ ifeq ($(CONFIG_UIO_IVSHMEM),y) CSRCS += ivshmem_uio.c endif +ifeq ($(CONFIG_IVSHMEM_FLAT),y) +CSRCS += ivshmem_flat.c +endif + DEPPATH += --dep-path ivshmem VPATH += :ivshmem diff --git a/drivers/ivshmem/ivshmem_flat.c b/drivers/ivshmem/ivshmem_flat.c new file mode 100644 index 00000000000..6db337e774d --- /dev/null +++ b/drivers/ivshmem/ivshmem_flat.c @@ -0,0 +1,118 @@ +/**************************************************************************** + * drivers/ivshmem/ivshmem_flat.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <debug.h> +#include <inttypes.h> + +#include <nuttx/kmalloc.h> +#include <nuttx/ivshmem.h> +#include <nuttx/ivshmem_flat.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef getreg32 +#define getreg32(a) (*(volatile uint32_t *)(a)) +#endif + +#ifndef putreg32 +#define putreg32(v,a) (*(volatile uint32_t *)(a) = (v)) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ivshmem_flat_kick_peer + ****************************************************************************/ + +static int ivshmem_flat_kick_peer(FAR struct ivshmem_device_s *dev, + uint32_t peer_id) +{ + /* Vector 0 in bits 0..15, peer id in bits 16..31 */ + + putreg32(peer_id << 16, (uintptr_t)dev->reg + IVSHMEM_REG_DOORBELL); + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ivshmem_flat_register + ****************************************************************************/ + +int ivshmem_flat_register(FAR const struct ivshmem_flat_config_s *config) +{ + FAR struct ivshmem_device_s *dev; + int ret; + + DEBUGASSERT(config != NULL && config->reg != 0 && config->shmem != 0); + + dev = kmm_zalloc(sizeof(*dev)); + if (dev == NULL) + { + return -ENOMEM; + } + + dev->id = config->id; + dev->reg = (FAR void *)config->reg; + dev->shmem = (FAR void *)config->shmem; + dev->shmem_size = config->shmem_size; + dev->irq = config->irq; + dev->vmid = IVSHMEM_INVALID_VMID; + + /* Like ivshmem-plain without an MSI-X BAR: no irq means no doorbell, so + * the drivers on top poll. + */ + + if (config->irq >= 0) + { + dev->vmid = getreg32((uintptr_t)dev->reg + IVSHMEM_REG_IVPOSITION); + if (dev->vmid != 0 && dev->vmid != 1) + { + _warn("vmid=%" PRIu32 " not 0/1, use polling mode\n", dev->vmid); + dev->vmid = IVSHMEM_INVALID_VMID; + } + } + + _info("reg=%p shmem=%p size=%zu irq=%d vmid=%" PRIu32 "\n", + dev->reg, dev->shmem, dev->shmem_size, dev->irq, dev->vmid); + + ret = ivshmem_register_device(dev, ivshmem_flat_kick_peer); + if (ret < 0) + { + kmm_free(dev); + } + + return ret; +} diff --git a/include/nuttx/ivshmem_flat.h b/include/nuttx/ivshmem_flat.h new file mode 100644 index 00000000000..99a5c30c0d3 --- /dev/null +++ b/include/nuttx/ivshmem_flat.h @@ -0,0 +1,80 @@ +/**************************************************************************** + * include/nuttx/ivshmem_flat.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_IVSHMEM_FLAT_H +#define __INCLUDE_NUTTX_IVSHMEM_FLAT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#ifdef CONFIG_IVSHMEM_FLAT + +#include <stddef.h> +#include <stdint.h> + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct ivshmem_flat_config_s +{ + uintptr_t reg; /* Register block base */ + uintptr_t shmem; /* Shared memory base */ + size_t shmem_size; /* Shared memory size */ + int irq; /* Doorbell irq, negative if none */ + int id; /* ivshmem bus id, negative to let the bus assign */ +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Name: ivshmem_flat_register + * + * Description: + * Register a QEMU ivshmem-flat device on the ivshmem bus. + * + * Input Parameters: + * config - Device resources; copied, may be on the stack. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int ivshmem_flat_register(FAR const struct ivshmem_flat_config_s *config); + +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_IVSHMEM_FLAT */ +#endif /* __INCLUDE_NUTTX_IVSHMEM_FLAT_H */
