xiaoxiang781216 commented on code in PR #7673: URL: https://github.com/apache/nuttx/pull/7673#discussion_r1033086125
########## include/nuttx/virtio/virtio.h: ########## @@ -0,0 +1,169 @@ +/**************************************************************************** + * include/nuttx/virtio/virtio.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_VIRTIO_VIRTIO_H +#define __INCLUDE_NUTTX_VIRTIO_VIRTIO_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdbool.h> +#include <stdint.h> +#include <arch/spinlock.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* See virtio-v1.1-csprd01.pdf */ + +#define VIRTIO_MAGIC 0x74726976 +#define VIRTIO_VERSION 0x2 /* NOTE: Legacy devices used 0x1 */ + +#define VIRTIO_DEV_NET 0x1 + +#define VIRTIO_STATUS_ACKNOWLEDGE (1) +#define VIRTIO_STATUS_DRIVER (2) +#define VIRTIO_STATUS_DRIVER_OK (4) +#define VIRTIO_STATUS_FEATURES_OK (8) + +#define VIRTQ_DESC_F_NEXT 1 /* marks a buffer as continuing */ +#define VIRTQ_DESC_F_WRITE 2 /* marks a buffer as device write-only */ + +#define mb() SP_DMB() + +#define getreg32(a) (*(volatile uint32_t *)(a)) Review Comment: should we add virtio_ prefix all mb/getreg32/putreg32 to avoid the potential conflict? ########## drivers/virtio/virtio-mmio.c: ########## @@ -0,0 +1,226 @@ +/**************************************************************************** + * drivers/virtio/virtio-mmio.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 <stdbool.h> +#include <debug.h> + +#include <nuttx/arch.h> +#include <nuttx/kmalloc.h> +#include <nuttx/virtio/virtio.h> + +#ifdef CONFIG_DRIVERS_VIRTIO_NET +# include <nuttx/virtio/virtio-net.h> +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ptr_to_uint64(x) ((uint64_t)(uintptr_t)(x)) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: virtq_create + ****************************************************************************/ + +FAR struct virtqueue *virtq_create(uint32_t len) +{ + FAR struct virtqueue *virtq = (FAR struct virtqueue *) + kmm_zalloc(sizeof(struct virtqueue)); + ASSERT(virtq); + + virtq->len = len; + + /* See: 2.6 Split Virtqueues */ + + virtq->desc = (FAR struct virtqueue_desc *) + kmm_memalign(16, 16 * len); + ASSERT(virtq->desc); + + virtq->avail = (FAR struct virtqueue_avail *) + kmm_memalign(2, 6 + 2 * len); + ASSERT(virtq->avail); + + virtq->used = (FAR struct virtqueue_used *) + kmm_memalign(4, 6 + 8 * len); + ASSERT(virtq->used); + + virtq->desc_virt = (FAR void **) + kmm_memalign(16, sizeof(FAR void *) * len); + ASSERT(virtq->desc_virt); + + _info("virtq=%p (len=%" PRId32 ")\n", virtq, len); + _info("virtq->desc=%p \n", virtq->desc); + _info("virtq->avail=%p \n", virtq->avail); + _info("virtq->used=%p \n", virtq->used); + + virtq->avail->idx = 0; + virtq->used->idx = 0; + virtq->last_used_idx = 0; + + return virtq; +} + +/**************************************************************************** + * Name: virtq_dev_init + ****************************************************************************/ + +static int virtio_dev_init(uintptr_t virt, uint32_t irq) +{ + FAR struct virtio_mmio_regs *regs = (FAR struct virtio_mmio_regs *)virt; + int ret = -1; + uint32_t val; + + _info("examine virtio at 0x%" PRIxPTR "\n", virt); + + val = getreg32(®s->magic_value); + + if (VIRTIO_MAGIC != val) + { + _err("error: virtio at 0x%" PRIxPTR + " had wrong magic value 0x%" PRIx32 "\n", virt, val); + return ret; + } + + val = getreg32(®s->version); + + if (VIRTIO_VERSION != val) + { + _err("error: virtio at 0x%" PRIxPTR + " had wrong version 0x%" PRIx32 "\n", virt, val); + return ret; + } + + /* Reset */ + + putreg32(0, ®s->status); + mb(); + + /* Set ack */ + + val = getreg32(®s->status) | VIRTIO_STATUS_ACKNOWLEDGE; + putreg32(val, ®s->status); + mb(); + + /* Set driver */ + + val = getreg32(®s->status) | VIRTIO_STATUS_DRIVER; + putreg32(val, ®s->status); + mb(); + + /* Check the device_id */ + + val = getreg32(®s->device_id); + + switch (val) + { +#ifdef CONFIG_DRIVERS_VIRTIO_NET + case VIRTIO_DEV_NET: + ret = virtnet_init(regs, irq); + break; +#endif + default: + _warn("unsupported device_id 0x%" PRIx32 "\n", val); + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: virtq_alloc_desc + ****************************************************************************/ + +uint32_t virtq_alloc_desc(FAR struct virtqueue *virtq, + uint32_t idx, FAR void *addr) +{ + uint32_t id = idx % virtq->len; + + _info("virtq=%p, idx=%" PRId32 "\n", virtq, idx); + + virtq->desc[id].addr = (uintptr_t)addr; + virtq->desc_virt[id] = addr; + return id; +} + +/**************************************************************************** + * Name: virtq_add_to_device + ****************************************************************************/ + +void virtq_add_to_device(FAR struct virtio_mmio_regs *regs, + FAR struct virtqueue *virtq, + uint32_t queue_sel) +{ + _info("==== queue_sel=%" PRId32 ", virtq->len=%" PRId32 "\n", + queue_sel, virtq->len); + + putreg32(queue_sel, ®s->queue_sel); + mb(); + + putreg32(virtq->len, ®s->queue_num); + + putreg32((uint32_t)(ptr_to_uint64(virtq->desc)), + ®s->queue_desc_low); + putreg32((uint32_t)(ptr_to_uint64(virtq->desc) >> 32), + ®s->queue_desc_high); + + putreg32((uint32_t)(ptr_to_uint64(virtq->avail)), + ®s->queue_avail_low); + putreg32((uint32_t)(ptr_to_uint64(virtq->avail) >> 32), + ®s->queue_avail_high); + + putreg32((uint32_t)(ptr_to_uint64(virtq->used)), + ®s->queue_used_low); + putreg32((uint32_t)(ptr_to_uint64(virtq->used) >> 32), + ®s->queue_used_high); + + mb(); + putreg32(1, ®s->queue_ready); +} + +/**************************************************************************** + * Name: virtio_init + ****************************************************************************/ + +void virtio_init(void) +{ + uintptr_t virtio = (uintptr_t)CONFIG_DRIVERS_VIRTIO_BASE; + uint32_t irq = CONFIG_DRIVERS_VIRTIO_IRQ; + uint32_t size = CONFIG_DRIVERS_VIRTIO_REGSIZE; + uint32_t i; + + for (i = 0; i < CONFIG_DRIVERS_VIRTIO_NUM; i++) + { + virtio_dev_init(virtio + size * i, irq + i); Review Comment: Can all devices share this rule for address and irq? ########## include/nuttx/virtio/virtio.h: ########## @@ -0,0 +1,169 @@ +/**************************************************************************** + * include/nuttx/virtio/virtio.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_VIRTIO_VIRTIO_H +#define __INCLUDE_NUTTX_VIRTIO_VIRTIO_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdbool.h> +#include <stdint.h> +#include <arch/spinlock.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* See virtio-v1.1-csprd01.pdf */ + +#define VIRTIO_MAGIC 0x74726976 +#define VIRTIO_VERSION 0x2 /* NOTE: Legacy devices used 0x1 */ + +#define VIRTIO_DEV_NET 0x1 + +#define VIRTIO_STATUS_ACKNOWLEDGE (1) +#define VIRTIO_STATUS_DRIVER (2) +#define VIRTIO_STATUS_DRIVER_OK (4) +#define VIRTIO_STATUS_FEATURES_OK (8) + +#define VIRTQ_DESC_F_NEXT 1 /* marks a buffer as continuing */ +#define VIRTQ_DESC_F_WRITE 2 /* marks a buffer as device write-only */ + +#define mb() SP_DMB() + +#define getreg32(a) (*(volatile uint32_t *)(a)) +#define putreg32(v,a) (*(volatile uint32_t *)(a) = (v)) Review Comment: add FAR for getreg32 and putreg32 ########## include/nuttx/virtio/virtio.h: ########## @@ -0,0 +1,169 @@ +/**************************************************************************** + * include/nuttx/virtio/virtio.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_VIRTIO_VIRTIO_H +#define __INCLUDE_NUTTX_VIRTIO_VIRTIO_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdbool.h> +#include <stdint.h> +#include <arch/spinlock.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* See virtio-v1.1-csprd01.pdf */ + +#define VIRTIO_MAGIC 0x74726976 +#define VIRTIO_VERSION 0x2 /* NOTE: Legacy devices used 0x1 */ + +#define VIRTIO_DEV_NET 0x1 + +#define VIRTIO_STATUS_ACKNOWLEDGE (1) +#define VIRTIO_STATUS_DRIVER (2) +#define VIRTIO_STATUS_DRIVER_OK (4) +#define VIRTIO_STATUS_FEATURES_OK (8) + +#define VIRTQ_DESC_F_NEXT 1 /* marks a buffer as continuing */ +#define VIRTQ_DESC_F_WRITE 2 /* marks a buffer as device write-only */ + +#define mb() SP_DMB() + +#define getreg32(a) (*(volatile uint32_t *)(a)) +#define putreg32(v,a) (*(volatile uint32_t *)(a) = (v)) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* Table 4.1 MMIO Device Register Layout */ + +struct virtio_mmio_regs +{ + uint32_t magic_value; /* offset:0x000 */ + uint32_t version; /* offset:0x004 */ + uint32_t device_id; /* offset:0x008 */ + uint32_t vendor_id; /* offset:0x00c */ + uint32_t device_features; /* offset:0x010 */ + uint32_t device_features_sel; /* offset:0x014 */ + uint32_t _reserved0[2]; + uint32_t driver_features; /* offset:0x020 */ + uint32_t driver_features_sel; /* offset:0x024 */ + uint32_t _reserved1[2]; + uint32_t queue_sel; /* offset:0x030 */ + uint32_t queue_num_max; /* offset:0x034 */ + uint32_t queue_num; /* offset:0x038 */ + uint32_t _reserved2[2]; + uint32_t queue_ready; /* offset:0x044 */ + uint32_t _reserved3[2]; + uint32_t queue_notify; /* offset:0x050 */ + uint32_t _reserved4[3]; + uint32_t interrupt_status; /* offset:0x060 */ + uint32_t interrupt_ack; /* offset:0x064 */ + uint32_t _reserved5[2]; + uint32_t status; /* offset:0x070 */ + uint32_t _reserved6[3]; + uint32_t queue_desc_low; /* offset:0x080 */ + uint32_t queue_desc_high; /* offset:0x084 */ + uint32_t _reserved7[2]; + uint32_t queue_avail_low; /* offset:0x090 */ + uint32_t queue_avail_high; /* offset:0x094 */ + uint32_t _reserved8[2]; + uint32_t queue_used_low; /* offset:0x0a0 */ + uint32_t queue_used_high; /* offset:0x0a4 */ + uint32_t _reserved9[21]; + uint32_t config_generation; /* offset:0x0fc */ + uint32_t config[0]; +}; + +struct virtqueue_desc +{ + uint64_t addr; + uint32_t len; + uint16_t flags; + uint16_t next; +}; + +struct virtqueue_avail +{ + uint16_t flags; + uint16_t idx; + uint16_t ring[0]; +}; + +struct virtqueue_used_elem +{ + uint32_t id; + uint32_t len; +}; + +struct virtqueue_used +{ + uint16_t flags; + uint16_t idx; + struct virtqueue_used_elem ring[0]; +}; + +struct virtqueue +{ + uint32_t len; + uint16_t last_used_idx; + + FAR struct virtqueue_desc *desc; + FAR struct virtqueue_avail *avail; + FAR struct virtqueue_used *used; + FAR uint16_t *avail_event; + FAR void **desc_virt; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +FAR struct virtqueue *virtq_create(uint32_t len); + +uint32_t virtq_alloc_desc(FAR struct virtqueue *virtq, + uint32_t id, + FAR void *addr); + +void virtq_add_to_device(FAR struct virtio_mmio_regs *regs, + FAR struct virtqueue *virtq, + uint32_t queue_sel); + +void virtio_init(void); Review Comment: let's unify the prefix to vrito_mmio_xxx? ########## drivers/virtio/virtio-mmio.c: ########## @@ -0,0 +1,226 @@ +/**************************************************************************** + * drivers/virtio/virtio-mmio.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 <stdbool.h> +#include <debug.h> + +#include <nuttx/arch.h> +#include <nuttx/kmalloc.h> +#include <nuttx/virtio/virtio.h> + +#ifdef CONFIG_DRIVERS_VIRTIO_NET +# include <nuttx/virtio/virtio-net.h> +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ptr_to_uint64(x) ((uint64_t)(uintptr_t)(x)) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: virtq_create + ****************************************************************************/ + +FAR struct virtqueue *virtq_create(uint32_t len) +{ + FAR struct virtqueue *virtq = (FAR struct virtqueue *) + kmm_zalloc(sizeof(struct virtqueue)); + ASSERT(virtq); + + virtq->len = len; + + /* See: 2.6 Split Virtqueues */ + + virtq->desc = (FAR struct virtqueue_desc *) + kmm_memalign(16, 16 * len); + ASSERT(virtq->desc); + + virtq->avail = (FAR struct virtqueue_avail *) + kmm_memalign(2, 6 + 2 * len); + ASSERT(virtq->avail); + + virtq->used = (FAR struct virtqueue_used *) + kmm_memalign(4, 6 + 8 * len); + ASSERT(virtq->used); + + virtq->desc_virt = (FAR void **) + kmm_memalign(16, sizeof(FAR void *) * len); + ASSERT(virtq->desc_virt); + + _info("virtq=%p (len=%" PRId32 ")\n", virtq, len); Review Comment: should we add new macro to debug.h ########## drivers/virtio/virtio-mmio.c: ########## @@ -0,0 +1,226 @@ +/**************************************************************************** + * drivers/virtio/virtio-mmio.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 <stdbool.h> +#include <debug.h> + +#include <nuttx/arch.h> +#include <nuttx/kmalloc.h> +#include <nuttx/virtio/virtio.h> + +#ifdef CONFIG_DRIVERS_VIRTIO_NET +# include <nuttx/virtio/virtio-net.h> +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ptr_to_uint64(x) ((uint64_t)(uintptr_t)(x)) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: virtq_create + ****************************************************************************/ + +FAR struct virtqueue *virtq_create(uint32_t len) +{ + FAR struct virtqueue *virtq = (FAR struct virtqueue *) + kmm_zalloc(sizeof(struct virtqueue)); + ASSERT(virtq); + + virtq->len = len; + + /* See: 2.6 Split Virtqueues */ + + virtq->desc = (FAR struct virtqueue_desc *) + kmm_memalign(16, 16 * len); + ASSERT(virtq->desc); + + virtq->avail = (FAR struct virtqueue_avail *) + kmm_memalign(2, 6 + 2 * len); + ASSERT(virtq->avail); + + virtq->used = (FAR struct virtqueue_used *) + kmm_memalign(4, 6 + 8 * len); + ASSERT(virtq->used); + + virtq->desc_virt = (FAR void **) + kmm_memalign(16, sizeof(FAR void *) * len); + ASSERT(virtq->desc_virt); + + _info("virtq=%p (len=%" PRId32 ")\n", virtq, len); + _info("virtq->desc=%p \n", virtq->desc); + _info("virtq->avail=%p \n", virtq->avail); + _info("virtq->used=%p \n", virtq->used); + + virtq->avail->idx = 0; + virtq->used->idx = 0; + virtq->last_used_idx = 0; + + return virtq; +} + +/**************************************************************************** + * Name: virtq_dev_init + ****************************************************************************/ + +static int virtio_dev_init(uintptr_t virt, uint32_t irq) +{ + FAR struct virtio_mmio_regs *regs = (FAR struct virtio_mmio_regs *)virt; + int ret = -1; Review Comment: -1 to ENODEV ########## drivers/virtio/Kconfig: ########## @@ -0,0 +1,50 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +menuconfig DRIVERS_VIRTIO + bool "Virtio Device Support" + depends on !OPENAMP + default n + ---help--- + +menuconfig DRIVERS_VIRTIO_NUM Review Comment: should we use more specific name(e.g. DRIVERS_VIRTIO_MMIO_NUM)? since all similar setting is specific for virtio-mmio transport(there are many other transport). ########## include/nuttx/virtio/virtio.h: ########## @@ -0,0 +1,169 @@ +/**************************************************************************** + * include/nuttx/virtio/virtio.h Review Comment: should we change file name to virito-mmio.h since it's tightly couple with mmio transport? Or i's better: 1. Split mmio specific macro and function to virtio-mmio.h 2. Keep the common virtio macro and function in virtio.h 3. Ensure virto net driver only use virtio macro and function not virtio mmio. ########## drivers/virtio/Kconfig: ########## @@ -0,0 +1,50 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +menuconfig DRIVERS_VIRTIO + bool "Virtio Device Support" + depends on !OPENAMP + default n + ---help--- + +menuconfig DRIVERS_VIRTIO_NUM Review Comment: let's use: ``` if DRIVERS_VIRTIO endif ``` to avoid the duplication of "depends on DRIVERS_VIRTIO". -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org