mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-06-12 20:24:15 +08:00
[bsp/qemu-virt64-aarch64] Add VirtIO-Console and GPIO driver (#6150)
This commit is contained in:
@@ -435,6 +435,7 @@ CONFIG_RT_LWIP_USING_PING=y
|
||||
# CONFIG_PKG_USING_RAPIDJSON is not set
|
||||
# CONFIG_PKG_USING_JSMN is not set
|
||||
# CONFIG_PKG_USING_AGILE_JSMN is not set
|
||||
# CONFIG_PKG_USING_PARSON is not set
|
||||
|
||||
#
|
||||
# XML: Extensible Markup Language
|
||||
@@ -548,15 +549,6 @@ CONFIG_RT_LWIP_USING_PING=y
|
||||
# CONFIG_PKG_USING_RT_KPRINTF_THREADSAFE is not set
|
||||
# CONFIG_PKG_USING_RT_VSNPRINTF_FULL is not set
|
||||
|
||||
#
|
||||
# POSIX extension functions
|
||||
#
|
||||
# CONFIG_PKG_USING_POSIX_GETLINE is not set
|
||||
# CONFIG_PKG_USING_POSIX_WCWIDTH is not set
|
||||
# CONFIG_PKG_USING_POSIX_ITOA is not set
|
||||
# CONFIG_PKG_USING_POSIX_STRINGS is not set
|
||||
# CONFIG_PKG_USING_POSIX_CTYPES is not set
|
||||
|
||||
#
|
||||
# acceleration: Assembly language or algorithmic acceleration packages
|
||||
#
|
||||
@@ -622,6 +614,7 @@ CONFIG_RT_LWIP_USING_PING=y
|
||||
# CONFIG_PKG_USING_REALTEK_AMEBA is not set
|
||||
# CONFIG_PKG_USING_SHT2X is not set
|
||||
# CONFIG_PKG_USING_SHT3X is not set
|
||||
# CONFIG_PKG_USING_ADT74XX is not set
|
||||
# CONFIG_PKG_USING_AS7341 is not set
|
||||
# CONFIG_PKG_USING_STM32_SDIO is not set
|
||||
# CONFIG_PKG_USING_ICM20608 is not set
|
||||
@@ -779,8 +772,10 @@ CONFIG_BSP_USING_UART=y
|
||||
CONFIG_RT_USING_UART0=y
|
||||
CONFIG_BSP_USING_RTC=y
|
||||
CONFIG_BSP_USING_ALARM=y
|
||||
CONFIG_BSP_USING_PIN=y
|
||||
CONFIG_BSP_USING_VIRTIO_BLK=y
|
||||
CONFIG_BSP_USING_VIRTIO_NET=y
|
||||
CONFIG_BSP_USING_VIRTIO_CONSOLE=y
|
||||
CONFIG_BSP_USING_VIRTIO_GPU=y
|
||||
CONFIG_BSP_USING_VIRTIO_INPUT=y
|
||||
CONFIG_BSP_USING_GIC=y
|
||||
|
||||
@@ -45,13 +45,20 @@ Hi, this is RT-Thread!!
|
||||
msh />
|
||||
```
|
||||
|
||||
Use VirtIO-Console in new terminal by:
|
||||
````
|
||||
telnet 127.0.0.1 4321
|
||||
````
|
||||
|
||||
## 4. Condition
|
||||
|
||||
| Driver | Condition | Remark |
|
||||
| ------ | --------- | ------ |
|
||||
| UART | Support | UART0 |
|
||||
| RTC | Support | - |
|
||||
| GPIO | Support | - |
|
||||
| VIRTIO BLK | Support | - |
|
||||
| VIRTIO NET | Support | - |
|
||||
| VIRTIO Console | Support | - |
|
||||
| VIRTIO GPU | Support | 2D |
|
||||
| VIRTIO Input | Support | Keyboard, Mouse, Tablet |
|
||||
@@ -46,13 +46,20 @@ Hi, this is RT-Thread!!
|
||||
msh />
|
||||
```
|
||||
|
||||
如果需要使用VirtIO-Console,请在新终端使用以下命令连接控制台:
|
||||
```
|
||||
telnet 127.0.0.1 4321
|
||||
```
|
||||
|
||||
## 4.支持情况
|
||||
|
||||
| 驱动 | 支持情况 | 备注 |
|
||||
| ------ | ---- | :------: |
|
||||
| UART | 支持 | UART0 |
|
||||
| RTC | 支持 | - |
|
||||
| GPIO | 支持 | - |
|
||||
| VIRTIO BLK | 支持 | - |
|
||||
| VIRTIO NET | 支持 | - |
|
||||
| VIRTIO Console | 支持 | - |
|
||||
| VIRTIO GPU | 支持 | 2D |
|
||||
| VIRTIO Input | 支持 | Keyboard, Mouse, Tablet |
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-11-11 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <virtio_console.h>
|
||||
|
||||
int console_init()
|
||||
{
|
||||
rt_err_t status = RT_EOK;
|
||||
rt_device_t device = rt_device_find("virtio-console0");
|
||||
|
||||
if (device != RT_NULL && rt_device_open(device, 0) == RT_EOK)
|
||||
{
|
||||
/* Create vport0p1 */
|
||||
status = rt_device_control(device, VIRTIO_DEVICE_CTRL_CONSOLE_PORT_CREATE, RT_NULL);
|
||||
}
|
||||
|
||||
if (device != RT_NULL)
|
||||
{
|
||||
rt_device_close(device);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
INIT_ENV_EXPORT(console_init);
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
|
||||
static int console(int argc, char **argv)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
if (!rt_strcmp(argv[1], "set"))
|
||||
{
|
||||
rt_kprintf("console change to %s\n", argv[2]);
|
||||
rt_console_set_device(argv[2]);
|
||||
finsh_set_device(argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Unknown command. Please enter 'console' for help\n");
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Usage: \n");
|
||||
rt_kprintf("console set <name> - change console by name\n");
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
MSH_CMD_EXPORT(console, set console name);
|
||||
|
||||
#endif /* FINSH_USING_MSH */
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-6-30 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_USING_PIN
|
||||
|
||||
void qemu_gpio3_key_poweroff(void *args)
|
||||
{
|
||||
rt_kprintf("\nYou power off the machine.\n");
|
||||
|
||||
rt_hw_cpu_shutdown();
|
||||
}
|
||||
|
||||
int pin_init()
|
||||
{
|
||||
rt_pin_attach_irq(3, PIN_IRQ_MODE_FALLING, qemu_gpio3_key_poweroff, RT_NULL);
|
||||
rt_pin_irq_enable(3, RT_TRUE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
INIT_ENV_EXPORT(pin_init);
|
||||
|
||||
#endif /* RT_USING_PIN */
|
||||
@@ -24,6 +24,11 @@ menu "AARCH64 qemu virt64 configs"
|
||||
default n
|
||||
endif
|
||||
|
||||
config BSP_USING_PIN
|
||||
bool "Using PIN"
|
||||
select RT_USING_PIN
|
||||
default y
|
||||
|
||||
config BSP_USING_VIRTIO_BLK
|
||||
bool "Using VirtIO BLK"
|
||||
default y
|
||||
@@ -32,6 +37,10 @@ menu "AARCH64 qemu virt64 configs"
|
||||
bool "Using VirtIO NET"
|
||||
default y
|
||||
|
||||
config BSP_USING_VIRTIO_CONSOLE
|
||||
bool "Using VirtIO Console"
|
||||
default y
|
||||
|
||||
config BSP_USING_VIRTIO_GPU
|
||||
bool "Using VirtIO GPU"
|
||||
default y
|
||||
|
||||
@@ -29,6 +29,7 @@ struct mem_desc platform_mem_desc[] =
|
||||
{
|
||||
{0x40000000, 0x80000000, 0x40000000, NORMAL_MEM},
|
||||
{PL031_RTC_BASE, PL031_RTC_BASE + 0x1000, PL031_RTC_BASE, DEVICE_MEM},
|
||||
{PL061_GPIO_BASE, PL061_GPIO_BASE + 0x1000, PL061_GPIO_BASE, DEVICE_MEM},
|
||||
{PL011_UART0_BASE, PL011_UART0_BASE + 0x1000, PL011_UART0_BASE, DEVICE_MEM},
|
||||
{VIRTIO_MMIO_BASE, VIRTIO_MMIO_BASE + VIRTIO_MAX_NR * VIRTIO_MMIO_SIZE, VIRTIO_MMIO_BASE, DEVICE_MEM},
|
||||
#ifdef BSP_USING_GICV2
|
||||
@@ -95,6 +96,13 @@ void poweroff(void)
|
||||
}
|
||||
MSH_CMD_EXPORT(poweroff, poweroff...);
|
||||
|
||||
void rt_hw_cpu_shutdown()
|
||||
{
|
||||
rt_kprintf("shutdown...\n");
|
||||
|
||||
poweroff();
|
||||
}
|
||||
|
||||
void reboot(void)
|
||||
{
|
||||
arm_psci_system_reboot();
|
||||
|
||||
@@ -0,0 +1,314 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-6-30 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include <board.h>
|
||||
|
||||
#include "drv_gpio.h"
|
||||
|
||||
#ifdef BSP_USING_PIN
|
||||
|
||||
#define GPIODIR 0x400
|
||||
#define GPIOIS 0x404
|
||||
#define GPIOIBE 0x408
|
||||
#define GPIOIEV 0x40c
|
||||
#define GPIOIE 0x410
|
||||
#define GPIORIS 0x414
|
||||
#define GPIOMIS 0x418
|
||||
#define GPIOIC 0x41c
|
||||
|
||||
#define BIT(x) (1 << (x))
|
||||
|
||||
#define PL061_GPIO_NR 8
|
||||
|
||||
static struct pl061
|
||||
{
|
||||
#ifdef RT_USING_SMP
|
||||
struct rt_spinlock spinlock;
|
||||
#endif
|
||||
void (*(hdr[PL061_GPIO_NR]))(void *args);
|
||||
void *args[PL061_GPIO_NR];
|
||||
} _pl061;
|
||||
|
||||
rt_inline rt_uint8_t pl061_read8(rt_ubase_t offset)
|
||||
{
|
||||
return HWREG8(PL061_GPIO_BASE + offset);
|
||||
}
|
||||
|
||||
rt_inline void pl061_write8(rt_ubase_t offset, rt_uint8_t value)
|
||||
{
|
||||
HWREG8(PL061_GPIO_BASE + offset) = value;
|
||||
}
|
||||
|
||||
static void pl061_pin_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
|
||||
{
|
||||
int value;
|
||||
rt_uint8_t gpiodir;
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_base_t level;
|
||||
#endif
|
||||
|
||||
if (pin < 0 || pin >= PL061_GPIO_NR)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
level = rt_spin_lock_irqsave(&_pl061.spinlock);
|
||||
#endif
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case PIN_MODE_OUTPUT:
|
||||
|
||||
value = !!pl061_read8((BIT(pin + 2)));
|
||||
|
||||
pl061_write8(BIT(pin + 2), 0 << pin);
|
||||
gpiodir = pl061_read8(GPIODIR);
|
||||
gpiodir |= BIT(pin);
|
||||
pl061_write8(GPIODIR, gpiodir);
|
||||
|
||||
/*
|
||||
* gpio value is set again, because pl061 doesn't allow to set value of
|
||||
* a gpio pin before configuring it in OUT mode.
|
||||
*/
|
||||
pl061_write8((BIT(pin + 2)), value << pin);
|
||||
|
||||
break;
|
||||
case PIN_MODE_INPUT:
|
||||
|
||||
gpiodir = pl061_read8(GPIODIR);
|
||||
gpiodir &= ~(BIT(pin));
|
||||
pl061_write8(GPIODIR, gpiodir);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_spin_unlock_irqrestore(&_pl061.spinlock, level);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void pl061_pin_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
|
||||
{
|
||||
pl061_write8(BIT(pin + 2), !!value << pin);
|
||||
}
|
||||
|
||||
static int pl061_pin_read(struct rt_device *device, rt_base_t pin)
|
||||
{
|
||||
return !!pl061_read8((BIT(pin + 2)));
|
||||
}
|
||||
|
||||
static rt_err_t pl061_pin_attach_irq(struct rt_device *device, rt_int32_t pin, rt_uint32_t mode, void (*hdr)(void *args), void *args)
|
||||
{
|
||||
rt_uint8_t gpiois, gpioibe, gpioiev;
|
||||
rt_uint8_t bit = BIT(mode);
|
||||
#ifdef RT_USING_SMP
|
||||
rt_base_t level;
|
||||
#endif
|
||||
|
||||
if (pin < 0 || pin >= PL061_GPIO_NR)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
level = rt_spin_lock_irqsave(&_pl061.spinlock);
|
||||
#endif
|
||||
|
||||
gpioiev = pl061_read8(GPIOIEV);
|
||||
gpiois = pl061_read8(GPIOIS);
|
||||
gpioibe = pl061_read8(GPIOIBE);
|
||||
|
||||
if (mode == PIN_IRQ_MODE_HIGH_LEVEL || pin == PIN_IRQ_MODE_LOW_LEVEL)
|
||||
{
|
||||
rt_bool_t polarity = (mode == PIN_IRQ_MODE_HIGH_LEVEL);
|
||||
|
||||
/* Disable edge detection */
|
||||
gpioibe &= ~bit;
|
||||
/* Enable level detection */
|
||||
gpiois |= bit;
|
||||
|
||||
/* Select polarity */
|
||||
if (polarity)
|
||||
{
|
||||
gpioiev |= bit;
|
||||
}
|
||||
else
|
||||
{
|
||||
gpioiev &= ~bit;
|
||||
}
|
||||
}
|
||||
else if (mode == PIN_IRQ_MODE_RISING_FALLING)
|
||||
{
|
||||
/* Disable level detection */
|
||||
gpiois &= ~bit;
|
||||
/* Select both edges, setting this makes GPIOEV be ignored */
|
||||
gpioibe |= bit;
|
||||
}
|
||||
else if (mode == PIN_IRQ_MODE_RISING || mode == PIN_IRQ_MODE_FALLING)
|
||||
{
|
||||
rt_bool_t rising = (mode == PIN_IRQ_MODE_RISING);
|
||||
|
||||
/* Disable level detection */
|
||||
gpiois &= ~bit;
|
||||
/* Clear detection on both edges */
|
||||
gpioibe &= ~bit;
|
||||
|
||||
/* Select edge */
|
||||
if (rising)
|
||||
{
|
||||
gpioiev |= bit;
|
||||
}
|
||||
else
|
||||
{
|
||||
gpioiev &= ~bit;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No trigger: disable everything */
|
||||
gpiois &= ~bit;
|
||||
gpioibe &= ~bit;
|
||||
gpioiev &= ~bit;
|
||||
}
|
||||
|
||||
pl061_write8(GPIOIS, gpiois);
|
||||
pl061_write8(GPIOIBE, gpioibe);
|
||||
pl061_write8(GPIOIEV, gpioiev);
|
||||
|
||||
_pl061.hdr[pin] = hdr;
|
||||
_pl061.args[pin] = args;
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_spin_unlock_irqrestore(&_pl061.spinlock, level);
|
||||
#endif
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t pl061_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
|
||||
{
|
||||
if (pin < 0 || pin >= PL061_GPIO_NR)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
_pl061.hdr[pin] = RT_NULL;
|
||||
_pl061.args[pin] = RT_NULL;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t pl061_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
|
||||
{
|
||||
rt_uint8_t mask = BIT(pin);
|
||||
rt_uint8_t gpioie;
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_base_t level;
|
||||
#endif
|
||||
|
||||
if (pin < 0 || pin >= PL061_GPIO_NR)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
level = rt_spin_lock_irqsave(&_pl061.spinlock);
|
||||
#endif
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
gpioie = pl061_read8(GPIOIE) | mask;
|
||||
}
|
||||
else
|
||||
{
|
||||
gpioie = pl061_read8(GPIOIE) & ~mask;
|
||||
}
|
||||
|
||||
pl061_write8(GPIOIE, gpioie);
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_spin_unlock_irqrestore(&_pl061.spinlock, level);
|
||||
#endif
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static const struct rt_pin_ops ops =
|
||||
{
|
||||
pl061_pin_mode,
|
||||
pl061_pin_write,
|
||||
pl061_pin_read,
|
||||
pl061_pin_attach_irq,
|
||||
pl061_pin_detach_irq,
|
||||
pl061_pin_irq_enable,
|
||||
RT_NULL,
|
||||
};
|
||||
|
||||
static void rt_hw_gpio_isr(int irqno, void *param)
|
||||
{
|
||||
rt_uint8_t mask;
|
||||
unsigned long pending;
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_base_t level;
|
||||
#endif
|
||||
|
||||
pending = pl061_read8(GPIOMIS);
|
||||
|
||||
if (pending)
|
||||
{
|
||||
rt_base_t pin;
|
||||
|
||||
for (pin = 0; pin < PL061_GPIO_NR; ++pin)
|
||||
{
|
||||
if (pending & BIT(pin))
|
||||
{
|
||||
mask |= BIT(pin);
|
||||
|
||||
if (_pl061.hdr[pin] != RT_NULL)
|
||||
{
|
||||
_pl061.hdr[pin](_pl061.args[pin]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
level = rt_spin_lock_irqsave(&_pl061.spinlock);
|
||||
#endif
|
||||
|
||||
pl061_write8(GPIOIC, mask);
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_spin_unlock_irqrestore(&_pl061.spinlock, level);
|
||||
#endif
|
||||
}
|
||||
|
||||
int rt_hw_gpio_init(void)
|
||||
{
|
||||
#ifdef RT_USING_SMP
|
||||
rt_spin_lock_init(&_pl061.spinlock);
|
||||
#endif
|
||||
|
||||
rt_device_pin_register("gpio", &ops, RT_NULL);
|
||||
rt_hw_interrupt_install(PL061_GPIO_IRQNUM, rt_hw_gpio_isr, RT_NULL, "gpio");
|
||||
rt_hw_interrupt_umask(PL061_GPIO_IRQNUM);
|
||||
|
||||
return 0;
|
||||
}
|
||||
INIT_DEVICE_EXPORT(rt_hw_gpio_init);
|
||||
|
||||
#endif /* BSP_USING_PIN */
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-6-30 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef DRV_GPIO_H__
|
||||
#define DRV_GPIO_H__
|
||||
|
||||
int rt_hw_gpio_init(void);
|
||||
|
||||
#endif
|
||||
@@ -17,6 +17,9 @@
|
||||
#ifdef BSP_USING_VIRTIO_NET
|
||||
#include <virtio_net.h>
|
||||
#endif
|
||||
#ifdef BSP_USING_VIRTIO_CONSOLE
|
||||
#include <virtio_console.h>
|
||||
#endif
|
||||
#ifdef BSP_USING_VIRTIO_GPU
|
||||
#include <virtio_gpu.h>
|
||||
#endif
|
||||
@@ -34,6 +37,9 @@ static virtio_device_init_handler virtio_device_init_handlers[] =
|
||||
#ifdef BSP_USING_VIRTIO_NET
|
||||
[VIRTIO_DEVICE_ID_NET] = rt_virtio_net_init,
|
||||
#endif
|
||||
#ifdef BSP_USING_VIRTIO_CONSOLE
|
||||
[VIRTIO_DEVICE_ID_CONSOLE] = rt_virtio_console_init,
|
||||
#endif
|
||||
#ifdef BSP_USING_VIRTIO_GPU
|
||||
[VIRTIO_DEVICE_ID_GPU] = rt_virtio_gpu_init,
|
||||
#endif
|
||||
|
||||
@@ -23,6 +23,11 @@
|
||||
#define PL031_RTC_SIZE 0x00001000
|
||||
#define PL031_RTC_IRQNUM (32 + 2)
|
||||
|
||||
/* GPIO */
|
||||
#define PL061_GPIO_BASE 0x09030000
|
||||
#define PL061_GPIO_SIZE 0x00001000
|
||||
#define PL061_GPIO_IRQNUM (32 + 7)
|
||||
|
||||
/* VirtIO */
|
||||
#define VIRTIO_MMIO_BASE 0x0a000000
|
||||
#define VIRTIO_MMIO_SIZE 0x00000200
|
||||
|
||||
@@ -54,11 +54,42 @@ void virtio_interrupt_ack(struct virtio_device *dev)
|
||||
}
|
||||
}
|
||||
|
||||
rt_bool_t virtio_has_feature(struct virtio_device *dev, rt_uint32_t feature_bit)
|
||||
{
|
||||
_virtio_dev_check(dev);
|
||||
|
||||
return !!(dev->mmio_config->device_features & (1UL << feature_bit));
|
||||
}
|
||||
|
||||
rt_err_t virtio_queues_alloc(struct virtio_device *dev, rt_size_t queues_num)
|
||||
{
|
||||
_virtio_dev_check(dev);
|
||||
|
||||
dev->queues = rt_malloc(sizeof(struct virtq) * queues_num);
|
||||
|
||||
if (dev->queues != RT_NULL)
|
||||
{
|
||||
dev->queues_num = queues_num;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
void virtio_queues_free(struct virtio_device *dev)
|
||||
{
|
||||
if (dev->queues != RT_NULL)
|
||||
{
|
||||
dev->queues_num = 0;
|
||||
rt_free(dev->queues);
|
||||
}
|
||||
}
|
||||
|
||||
rt_err_t virtio_queue_init(struct virtio_device *dev, rt_uint32_t queue_index, rt_size_t ring_size)
|
||||
{
|
||||
int i;
|
||||
void *pages;
|
||||
rt_ubase_t pages_paddr;
|
||||
rt_size_t pages_total_size;
|
||||
struct virtq *queue;
|
||||
|
||||
@@ -90,17 +121,16 @@ rt_err_t virtio_queue_init(struct virtio_device *dev, rt_uint32_t queue_index, r
|
||||
}
|
||||
|
||||
rt_memset(pages, 0, pages_total_size);
|
||||
pages_paddr = VIRTIO_VA2PA(pages);
|
||||
|
||||
dev->mmio_config->guest_page_size = VIRTIO_PAGE_SIZE;
|
||||
dev->mmio_config->queue_sel = queue_index;
|
||||
dev->mmio_config->queue_num = ring_size;
|
||||
dev->mmio_config->queue_align = VIRTIO_PAGE_SIZE;
|
||||
dev->mmio_config->queue_pfn = pages_paddr >> VIRTIO_PAGE_SHIFT;
|
||||
dev->mmio_config->queue_pfn = VIRTIO_VA2PA(pages) >> VIRTIO_PAGE_SHIFT;
|
||||
|
||||
queue->num = ring_size;
|
||||
queue->desc = (struct virtq_desc *)pages_paddr;
|
||||
queue->avail = (struct virtq_avail *)(pages_paddr + VIRTQ_DESC_TOTAL_SIZE(ring_size));
|
||||
queue->desc = (struct virtq_desc *)((rt_ubase_t)pages);
|
||||
queue->avail = (struct virtq_avail *)(((rt_ubase_t)pages) + VIRTQ_DESC_TOTAL_SIZE(ring_size));
|
||||
queue->used = (struct virtq_used *)VIRTIO_PAGE_ALIGN(
|
||||
(rt_ubase_t)&queue->avail->ring[ring_size] + VIRTQ_AVAIL_RES_SIZE);
|
||||
|
||||
@@ -175,7 +205,7 @@ rt_uint16_t virtio_alloc_desc(struct virtio_device *dev, rt_uint32_t queue_index
|
||||
|
||||
_virtio_dev_check(dev);
|
||||
|
||||
RT_ASSERT(queue_index < RT_USING_VIRTIO_QUEUE_MAX_NR);
|
||||
RT_ASSERT(queue_index < dev->queues_num);
|
||||
|
||||
queue = &dev->queues[queue_index];
|
||||
|
||||
@@ -206,7 +236,7 @@ void virtio_free_desc(struct virtio_device *dev, rt_uint32_t queue_index, rt_uin
|
||||
|
||||
queue = &dev->queues[queue_index];
|
||||
|
||||
RT_ASSERT(queue_index + 1 < RT_USING_VIRTIO_QUEUE_MAX_NR);
|
||||
RT_ASSERT(queue_index < dev->queues_num);
|
||||
RT_ASSERT(!queue->free[desc_index]);
|
||||
|
||||
queue->desc[desc_index].addr = 0;
|
||||
|
||||
@@ -19,10 +19,6 @@
|
||||
#define RT_USING_VIRTIO_VERSION 0x1
|
||||
#endif
|
||||
|
||||
#ifndef RT_USING_VIRTIO_QUEUE_MAX_NR
|
||||
#define RT_USING_VIRTIO_QUEUE_MAX_NR 4
|
||||
#endif
|
||||
|
||||
#include <virtio_mmio.h>
|
||||
#include <virtio_queue.h>
|
||||
|
||||
@@ -102,7 +98,9 @@ struct virtio_device
|
||||
{
|
||||
rt_uint32_t irq;
|
||||
|
||||
struct virtq queues[RT_USING_VIRTIO_QUEUE_MAX_NR];
|
||||
struct virtq *queues;
|
||||
rt_size_t queues_num;
|
||||
|
||||
union
|
||||
{
|
||||
rt_ubase_t *mmio_base;
|
||||
@@ -122,7 +120,10 @@ void virtio_reset_device(struct virtio_device *dev);
|
||||
void virtio_status_acknowledge_driver(struct virtio_device *dev);
|
||||
void virtio_status_driver_ok(struct virtio_device *dev);
|
||||
void virtio_interrupt_ack(struct virtio_device *dev);
|
||||
rt_bool_t virtio_has_feature(struct virtio_device *dev, rt_uint32_t feature_bit);
|
||||
|
||||
rt_err_t virtio_queues_alloc(struct virtio_device *dev, rt_size_t queues_num);
|
||||
void virtio_queues_free(struct virtio_device *dev);
|
||||
rt_err_t virtio_queue_init(struct virtio_device *dev, rt_uint32_t queue_index, rt_size_t ring_size);
|
||||
void virtio_queue_destroy(struct virtio_device *dev, rt_uint32_t queue_index);
|
||||
void virtio_queue_notify(struct virtio_device *dev, rt_uint32_t queue_index);
|
||||
|
||||
@@ -29,6 +29,14 @@ static void virtio_blk_rw(struct virtio_blk_device *virtio_blk_dev, rt_off_t pos
|
||||
/* Allocate 3 descriptors */
|
||||
while (virtio_alloc_desc_chain(virtio_dev, 0, 3, idx))
|
||||
{
|
||||
#ifdef RT_USING_SMP
|
||||
rt_spin_unlock_irqrestore(&virtio_dev->spinlock, level);
|
||||
#endif
|
||||
rt_thread_yield();
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
level = rt_spin_lock_irqsave(&virtio_dev->spinlock);
|
||||
#endif
|
||||
}
|
||||
|
||||
virtio_blk_dev->info[idx[0]].status = 0xff;
|
||||
@@ -150,7 +158,6 @@ static void virtio_blk_isr(int irqno, void *param)
|
||||
|
||||
/* Done with buffer */
|
||||
virtio_blk_dev->info[id].valid = RT_FALSE;
|
||||
rt_thread_yield();
|
||||
|
||||
queue->used_idx++;
|
||||
}
|
||||
@@ -200,12 +207,15 @@ rt_err_t rt_virtio_blk_init(rt_ubase_t *mmio_base, rt_uint32_t irq)
|
||||
/* Tell device that feature negotiation is complete and we're completely ready */
|
||||
virtio_status_driver_ok(virtio_dev);
|
||||
|
||||
if (virtio_queues_alloc(virtio_dev, 1) != RT_EOK)
|
||||
{
|
||||
goto _alloc_fail;
|
||||
}
|
||||
|
||||
/* Initialize queue 0 */
|
||||
if (virtio_queue_init(virtio_dev, 0, VIRTIO_BLK_QUEUE_RING_SIZE) != RT_EOK)
|
||||
{
|
||||
rt_free(virtio_blk_dev);
|
||||
|
||||
return -RT_ENOMEM;
|
||||
goto _alloc_fail;
|
||||
}
|
||||
|
||||
virtio_blk_dev->parent.type = RT_Device_Class_Block;
|
||||
@@ -217,5 +227,14 @@ rt_err_t rt_virtio_blk_init(rt_ubase_t *mmio_base, rt_uint32_t irq)
|
||||
rt_hw_interrupt_umask(irq);
|
||||
|
||||
return rt_device_register((rt_device_t)virtio_blk_dev, dev_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE);
|
||||
|
||||
_alloc_fail:
|
||||
|
||||
if (virtio_blk_dev != RT_NULL)
|
||||
{
|
||||
virtio_queues_free(virtio_dev);
|
||||
rt_free(virtio_blk_dev);
|
||||
}
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
#endif /* BSP_USING_VIRTIO_BLK */
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2021-11-11 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#ifndef __VIRTIO_CONSOLE_H__
|
||||
#define __VIRTIO_CONSOLE_H__
|
||||
|
||||
#include <rtdef.h>
|
||||
|
||||
#include <virtio.h>
|
||||
|
||||
#ifndef RT_USING_VIRTIO_CONSOLE_PORT_MAX_NR
|
||||
#define RT_USING_VIRTIO_CONSOLE_PORT_MAX_NR 4
|
||||
#endif
|
||||
|
||||
#define VIRTIO_CONSOLE_QUEUE_DATA_RX 0
|
||||
#define VIRTIO_CONSOLE_QUEUE_DATA_TX 1
|
||||
#define VIRTIO_CONSOLE_QUEUE_CTRL_RX 2
|
||||
#define VIRTIO_CONSOLE_QUEUE_CTRL_TX 3
|
||||
#define VIRTIO_CONSOLE_QUEUE_SIZE 64
|
||||
|
||||
/* Every port has data rx & tx, and port0 has ctrl rx & tx in multiport */
|
||||
#define VIRTIO_CONSOLE_PORT_QUEUE_INDEX(id, queue) ((id) * 2 + (!!(id)) * 2 + (queue))
|
||||
|
||||
#define VIRTIO_CONSOLE_PORT_BAD_ID (~(rt_uint32_t)0)
|
||||
|
||||
#define VIRTIO_CONSOLE_F_SIZE 0 /* Does host provide console size? */
|
||||
#define VIRTIO_CONSOLE_F_MULTIPORT 1 /* Does host provide multiple ports? */
|
||||
#define VIRTIO_CONSOLE_F_EMERG_WRITE 2 /* Does host support emergency write? */
|
||||
|
||||
struct virtio_console_config
|
||||
{
|
||||
rt_uint16_t cols;
|
||||
rt_uint16_t rows;
|
||||
rt_uint32_t max_nr_ports;
|
||||
rt_uint32_t emerg_wr;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct virtio_console_control
|
||||
{
|
||||
rt_uint32_t id; /* Port number */
|
||||
rt_uint16_t event; /* The kind of control event */
|
||||
rt_uint16_t value; /* Extra information for the event */
|
||||
};
|
||||
|
||||
enum virtio_console_control_event
|
||||
{
|
||||
VIRTIO_CONSOLE_DEVICE_READY = 0,
|
||||
VIRTIO_CONSOLE_PORT_ADD,
|
||||
VIRTIO_CONSOLE_PORT_REMOVE,
|
||||
VIRTIO_CONSOLE_PORT_READY,
|
||||
VIRTIO_CONSOLE_CONSOLE_PORT,
|
||||
VIRTIO_CONSOLE_RESIZE,
|
||||
VIRTIO_CONSOLE_PORT_OPEN,
|
||||
VIRTIO_CONSOLE_PORT_NAME,
|
||||
};
|
||||
|
||||
struct virtio_console_resize
|
||||
{
|
||||
rt_uint16_t cols;
|
||||
rt_uint16_t rows;
|
||||
};
|
||||
|
||||
struct virtio_console_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
struct virtio_device virtio_dev;
|
||||
|
||||
rt_uint32_t console_id;
|
||||
rt_size_t port_nr;
|
||||
rt_size_t max_port_nr;
|
||||
rt_list_t port_head;
|
||||
struct virtio_console_config *config;
|
||||
|
||||
struct
|
||||
{
|
||||
struct virtio_console_control rx_ctrl, tx_ctrl;
|
||||
} info[VIRTIO_CONSOLE_QUEUE_SIZE];
|
||||
};
|
||||
|
||||
rt_err_t rt_virtio_console_init(rt_ubase_t *mmio_base, rt_uint32_t irq);
|
||||
|
||||
enum
|
||||
{
|
||||
VIRTIO_DEVICE_CTRL_CONSOLE_PORT_CREATE = 0x20,
|
||||
VIRTIO_DEVICE_CTRL_CONSOLE_PORT_DESTROY,
|
||||
};
|
||||
|
||||
#endif /* __VIRTIO_CONSOLE_H__ */
|
||||
@@ -74,6 +74,14 @@ static void virtio_gpu_ctrl_send_command(struct virtio_gpu_device *virtio_gpu_de
|
||||
|
||||
while (virtio_alloc_desc_chain(virtio_dev, VIRTIO_GPU_QUEUE_CTRL, 2, idx))
|
||||
{
|
||||
#ifdef RT_USING_SMP
|
||||
rt_spin_unlock_irqrestore(&virtio_dev->spinlock, level);
|
||||
#endif
|
||||
rt_thread_yield();
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
level = rt_spin_lock_irqsave(&virtio_dev->spinlock);
|
||||
#endif
|
||||
}
|
||||
|
||||
rt_hw_dsb();
|
||||
@@ -126,7 +134,17 @@ static void virtio_gpu_cursor_send_command(struct virtio_gpu_device *virtio_gpu_
|
||||
rt_base_t level = rt_spin_lock_irqsave(&virtio_dev->spinlock);
|
||||
#endif
|
||||
|
||||
id = virtio_alloc_desc(virtio_dev, VIRTIO_GPU_QUEUE_CURSOR);
|
||||
while ((id = virtio_alloc_desc(virtio_dev, VIRTIO_GPU_QUEUE_CURSOR)) == VIRTQ_INVALID_DESC_ID)
|
||||
{
|
||||
#ifdef RT_USING_SMP
|
||||
rt_spin_unlock_irqrestore(&virtio_dev->spinlock, level);
|
||||
#endif
|
||||
rt_thread_yield();
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
level = rt_spin_lock_irqsave(&virtio_dev->spinlock);
|
||||
#endif
|
||||
}
|
||||
|
||||
addr = &virtio_gpu_dev->info[id].cursor_cmd;
|
||||
virtio_gpu_dev->info[id].cursor_valid = RT_TRUE;
|
||||
@@ -806,7 +824,6 @@ static void virtio_gpu_isr(int irqno, void *param)
|
||||
id = queue_ctrl->used->ring[queue_ctrl->used_idx % queue_ctrl->num].id;
|
||||
|
||||
virtio_gpu_dev->info[id].ctrl_valid = RT_FALSE;
|
||||
rt_thread_yield();
|
||||
|
||||
queue_ctrl->used_idx++;
|
||||
}
|
||||
@@ -817,7 +834,6 @@ static void virtio_gpu_isr(int irqno, void *param)
|
||||
id = queue_cursor->used->ring[queue_cursor->used_idx % queue_cursor->num].id;
|
||||
|
||||
virtio_gpu_dev->info[id].cursor_valid = RT_FALSE;
|
||||
rt_thread_yield();
|
||||
|
||||
queue_cursor->used_idx++;
|
||||
}
|
||||
@@ -866,6 +882,11 @@ rt_err_t rt_virtio_gpu_init(rt_ubase_t *mmio_base, rt_uint32_t irq)
|
||||
|
||||
virtio_status_driver_ok(virtio_dev);
|
||||
|
||||
if (virtio_queues_alloc(virtio_dev, 2) != RT_EOK)
|
||||
{
|
||||
goto _alloc_fail;
|
||||
}
|
||||
|
||||
if (virtio_queue_init(virtio_dev, VIRTIO_GPU_QUEUE_CTRL, VIRTIO_GPU_QUEUE_SIZE) != RT_EOK)
|
||||
{
|
||||
goto _alloc_fail;
|
||||
@@ -896,6 +917,7 @@ _alloc_fail:
|
||||
|
||||
if (virtio_gpu_dev != RT_NULL)
|
||||
{
|
||||
virtio_queues_free(virtio_dev);
|
||||
rt_free(virtio_gpu_dev);
|
||||
}
|
||||
return -RT_ENOMEM;
|
||||
|
||||
@@ -372,6 +372,11 @@ rt_err_t rt_virtio_input_init(rt_ubase_t *mmio_base, rt_uint32_t irq)
|
||||
|
||||
virtio_status_driver_ok(virtio_dev);
|
||||
|
||||
if (virtio_queues_alloc(virtio_dev, 2) != RT_EOK)
|
||||
{
|
||||
goto _alloc_fail;
|
||||
}
|
||||
|
||||
if (virtio_queue_init(virtio_dev, VIRTIO_INPUT_QUEUE_EVENT, VIRTIO_INPUT_EVENT_QUEUE_SIZE) != RT_EOK)
|
||||
{
|
||||
goto _alloc_fail;
|
||||
@@ -425,6 +430,7 @@ _alloc_fail:
|
||||
|
||||
if (virtio_input_dev != RT_NULL)
|
||||
{
|
||||
virtio_queues_free(virtio_dev);
|
||||
rt_free(virtio_input_dev);
|
||||
}
|
||||
return -RT_ENOMEM;
|
||||
|
||||
@@ -269,6 +269,11 @@ rt_err_t rt_virtio_net_init(rt_ubase_t *mmio_base, rt_uint32_t irq)
|
||||
|
||||
virtio_status_driver_ok(virtio_dev);
|
||||
|
||||
if (virtio_queues_alloc(virtio_dev, 2) != RT_EOK)
|
||||
{
|
||||
goto _alloc_fail;
|
||||
}
|
||||
|
||||
if (virtio_queue_init(virtio_dev, VIRTIO_NET_QUEUE_RX, VIRTIO_NET_RTX_QUEUE_SIZE) != RT_EOK)
|
||||
{
|
||||
goto _alloc_fail;
|
||||
@@ -301,6 +306,7 @@ _alloc_fail:
|
||||
|
||||
if (virtio_net_dev != RT_NULL)
|
||||
{
|
||||
virtio_queues_free(virtio_dev);
|
||||
rt_free(virtio_net_dev);
|
||||
}
|
||||
return -RT_ENOMEM;
|
||||
|
||||
@@ -9,4 +9,5 @@ qemu-system-aarch64 -M virt,gic-version=2 -cpu cortex-a53 -smp 4 -kernel rtthrea
|
||||
-device virtio-gpu-device,xres=800,yres=600,bus=virtio-mmio-bus.2 ^
|
||||
-device virtio-keyboard-device,bus=virtio-mmio-bus.3 ^
|
||||
-device virtio-mouse-device,bus=virtio-mmio-bus.4 ^
|
||||
-device virtio-tablet-device,bus=virtio-mmio-bus.5
|
||||
-device virtio-tablet-device,bus=virtio-mmio-bus.5 ^
|
||||
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
|
||||
|
||||
@@ -7,4 +7,5 @@ qemu-system-aarch64 -M virt,gic-version=2 -cpu cortex-a53 -smp 4 -kernel rtthrea
|
||||
-device virtio-gpu-device,xres=800,yres=600,bus=virtio-mmio-bus.2 \
|
||||
-device virtio-keyboard-device,bus=virtio-mmio-bus.3 \
|
||||
-device virtio-mouse-device,bus=virtio-mmio-bus.4 \
|
||||
-device virtio-tablet-device,bus=virtio-mmio-bus.5
|
||||
-device virtio-tablet-device,bus=virtio-mmio-bus.5 \
|
||||
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
|
||||
|
||||
@@ -5,4 +5,5 @@ qemu-img create -f raw sd.bin 64M
|
||||
:run
|
||||
qemu-system-aarch64 -M virt,gic-version=2 -cpu cortex-a53 -smp 4 -kernel rtthread.elf -nographic ^
|
||||
-drive if=none,file=sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 ^
|
||||
-netdev user,id=net0 -device virtio-net-device,netdev=net0,bus=virtio-mmio-bus.1
|
||||
-netdev user,id=net0 -device virtio-net-device,netdev=net0,bus=virtio-mmio-bus.1 ^
|
||||
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
|
||||
|
||||
@@ -3,4 +3,5 @@ dd if=/dev/zero of=sd.bin bs=1024 count=65536
|
||||
fi
|
||||
qemu-system-aarch64 -M virt,gic-version=2 -cpu cortex-a53 -smp 4 -kernel rtthread.elf -nographic \
|
||||
-drive if=none,file=sd.bin,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
|
||||
-netdev user,id=net0 -device virtio-net-device,netdev=net0,bus=virtio-mmio-bus.1
|
||||
-netdev user,id=net0 -device virtio-net-device,netdev=net0,bus=virtio-mmio-bus.1 \
|
||||
-device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0
|
||||
|
||||
@@ -241,9 +241,6 @@
|
||||
/* enhanced kernel services */
|
||||
|
||||
|
||||
/* POSIX extension functions */
|
||||
|
||||
|
||||
/* acceleration: Assembly language or algorithmic acceleration packages */
|
||||
|
||||
|
||||
@@ -276,8 +273,10 @@
|
||||
#define RT_USING_UART0
|
||||
#define BSP_USING_RTC
|
||||
#define BSP_USING_ALARM
|
||||
#define BSP_USING_PIN
|
||||
#define BSP_USING_VIRTIO_BLK
|
||||
#define BSP_USING_VIRTIO_NET
|
||||
#define BSP_USING_VIRTIO_CONSOLE
|
||||
#define BSP_USING_VIRTIO_GPU
|
||||
#define BSP_USING_VIRTIO_INPUT
|
||||
#define BSP_USING_GIC
|
||||
|
||||
Reference in New Issue
Block a user