|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Wind River Systems, Inc. |
| 3 | + * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007 |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef OPENAMP_VIRTIO_MMIO_H |
| 9 | +#define OPENAMP_VIRTIO_MMIO_H |
| 10 | + |
| 11 | +#include <metal/device.h> |
| 12 | +#include <openamp/virtio.h> |
| 13 | +#include <openamp/virtqueue.h> |
| 14 | + |
| 15 | +#ifdef __cplusplus |
| 16 | +extern "C" { |
| 17 | +#endif |
| 18 | + |
| 19 | +/* Enable support for legacy devices */ |
| 20 | +#define VIRTIO_MMIO_LEGACY |
| 21 | + |
| 22 | +/* Control registers */ |
| 23 | + |
| 24 | +/* Magic value ("virt" string) - Read Only */ |
| 25 | +#define VIRTIO_MMIO_MAGIC_VALUE 0x000 |
| 26 | + |
| 27 | +#define VIRTIO_MMIO_MAGIC_VALUE_STRING ('v' | ('i' << 8) | ('r' << 16) | ('t' << 24)) |
| 28 | + |
| 29 | +/* Virtio device version - Read Only */ |
| 30 | +#define VIRTIO_MMIO_VERSION 0x004 |
| 31 | + |
| 32 | +/* Virtio device ID - Read Only */ |
| 33 | +#define VIRTIO_MMIO_DEVICE_ID 0x008 |
| 34 | + |
| 35 | +/* Virtio vendor ID - Read Only */ |
| 36 | +#define VIRTIO_MMIO_VENDOR_ID 0x00c |
| 37 | + |
| 38 | +/* |
| 39 | + * Bitmask of the features supported by the device (host) |
| 40 | + * (32 bits per set) - Read Only |
| 41 | + */ |
| 42 | +#define VIRTIO_MMIO_DEVICE_FEATURES 0x010 |
| 43 | + |
| 44 | +/* Device (host) features set selector - Write Only */ |
| 45 | +#define VIRTIO_MMIO_DEVICE_FEATURES_SEL 0x014 |
| 46 | + |
| 47 | +/* |
| 48 | + * Bitmask of features activated by the driver (guest) |
| 49 | + * (32 bits per set) - Write Only |
| 50 | + */ |
| 51 | +#define VIRTIO_MMIO_DRIVER_FEATURES 0x020 |
| 52 | + |
| 53 | +/* Activated features set selector - Write Only */ |
| 54 | +#define VIRTIO_MMIO_DRIVER_FEATURES_SEL 0x024 |
| 55 | + |
| 56 | +#ifndef VIRTIO_MMIO_NO_LEGACY /* LEGACY DEVICES ONLY! */ |
| 57 | +/* Guest's memory page size in bytes - Write Only */ |
| 58 | +#define VIRTIO_MMIO_GUEST_PAGE_SIZE 0x028 |
| 59 | +#endif |
| 60 | + |
| 61 | +/* Queue selector - Write Only */ |
| 62 | +#define VIRTIO_MMIO_QUEUE_SEL 0x030 |
| 63 | + |
| 64 | +/* Maximum size of the currently selected queue - Read Only */ |
| 65 | +#define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034 |
| 66 | + |
| 67 | +/* Queue size for the currently selected queue - Write Only */ |
| 68 | +#define VIRTIO_MMIO_QUEUE_NUM 0x038 |
| 69 | + |
| 70 | +#ifdef VIRTIO_MMIO_LEGACY |
| 71 | +/* Used Ring alignment for the currently selected queue - Write Only */ |
| 72 | +#define VIRTIO_MMIO_QUEUE_ALIGN 0x03c |
| 73 | +/* Guest's PFN for the currently selected queue - Read Write */ |
| 74 | +#define VIRTIO_MMIO_QUEUE_PFN 0x040 |
| 75 | +#endif |
| 76 | + |
| 77 | +/* Ready bit for the currently selected queue - Read Write */ |
| 78 | +#define VIRTIO_MMIO_QUEUE_READY 0x044 |
| 79 | + |
| 80 | +/* Queue notifier - Write Only */ |
| 81 | +#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050 |
| 82 | + |
| 83 | +/* Interrupt status - Read Only */ |
| 84 | +#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 |
| 85 | + |
| 86 | +/* Interrupt acknowledge - Write Only */ |
| 87 | +#define VIRTIO_MMIO_INTERRUPT_ACK 0x064 |
| 88 | + |
| 89 | +/* Device status register - Read Write */ |
| 90 | +#define VIRTIO_MMIO_STATUS 0x070 |
| 91 | + |
| 92 | +/* Selected queue's Descriptor Table address, 64 bits in two halves */ |
| 93 | +#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 |
| 94 | +#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084 |
| 95 | + |
| 96 | +/* Selected queue's Available Ring address, 64 bits in two halves */ |
| 97 | +#define VIRTIO_MMIO_QUEUE_AVAIL_LOW 0x090 |
| 98 | +#define VIRTIO_MMIO_QUEUE_AVAIL_HIGH 0x094 |
| 99 | + |
| 100 | +/* Selected queue's Used Ring address, 64 bits in two halves */ |
| 101 | +#define VIRTIO_MMIO_QUEUE_USED_LOW 0x0a0 |
| 102 | +#define VIRTIO_MMIO_QUEUE_USED_HIGH 0x0a4 |
| 103 | + |
| 104 | +/* Shared memory region id */ |
| 105 | +#define VIRTIO_MMIO_SHM_SEL 0x0ac |
| 106 | + |
| 107 | +/* Shared memory region length, 64 bits in two halves */ |
| 108 | +#define VIRTIO_MMIO_SHM_LEN_LOW 0x0b0 |
| 109 | +#define VIRTIO_MMIO_SHM_LEN_HIGH 0x0b4 |
| 110 | + |
| 111 | +/* Shared memory region base address, 64 bits in two halves */ |
| 112 | +#define VIRTIO_MMIO_SHM_BASE_LOW 0x0b8 |
| 113 | +#define VIRTIO_MMIO_SHM_BASE_HIGH 0x0bc |
| 114 | + |
| 115 | +/* Configuration atomicity value */ |
| 116 | +#define VIRTIO_MMIO_CONFIG_GENERATION 0x0fc |
| 117 | + |
| 118 | +/* |
| 119 | + * The config space is defined by each driver as |
| 120 | + * the per-driver configuration space - Read Write |
| 121 | + */ |
| 122 | +#define VIRTIO_MMIO_CONFIG 0x100 |
| 123 | + |
| 124 | +/* Interrupt flags (re: interrupt status & acknowledge registers) */ |
| 125 | +#define VIRTIO_MMIO_INT_VRING (1 << 0) |
| 126 | +#define VIRTIO_MMIO_INT_CONFIG (1 << 1) |
| 127 | + |
| 128 | +/* Data buffer size for preallocated buffers before vring */ |
| 129 | +#define VIRTIO_MMIO_MAX_DATA_SIZE 128 |
| 130 | + |
| 131 | +/** |
| 132 | + * struct virtio_mmio_dev_mem: VIRTIO MMIO memory area |
| 133 | + * @base memory region physical address |
| 134 | + * @size memory region size |
| 135 | + */ |
| 136 | +struct virtio_mmio_dev_mem { |
| 137 | + void *base; |
| 138 | + size_t size; |
| 139 | +}; |
| 140 | + |
| 141 | +/** |
| 142 | + * struct virtio_mmio_device: representation of a VIRTIO MMIO device |
| 143 | + * @vdev base virtio device struct |
| 144 | + * @cfg_io device configuration space metal_io_region |
| 145 | + * @shm_io pre-shared memory space metal_io_region |
| 146 | + * @shm_device shared memory device |
| 147 | + * @cfg_mem VIRTIO device configuration space |
| 148 | + * @shm_mem VIRTIO device pre-shared memory |
| 149 | + * @device_mode VIRTIO_DEV_DRIVER or VIRTIO_DEV_DEVICE |
| 150 | + * @irq interrupt number |
| 151 | + * @user_data custom user data |
| 152 | + */ |
| 153 | +struct virtio_mmio_device { |
| 154 | + struct virtio_device vdev; |
| 155 | + struct metal_io_region *cfg_io; |
| 156 | + struct metal_io_region *shm_io; |
| 157 | + struct metal_device shm_device; |
| 158 | + struct virtio_mmio_dev_mem cfg_mem; |
| 159 | + struct virtio_mmio_dev_mem shm_mem; |
| 160 | + unsigned int device_mode; |
| 161 | + unsigned int irq; |
| 162 | + void *user_data; |
| 163 | +}; |
| 164 | + |
| 165 | +/** |
| 166 | + * @brief Register a VIRTIO device with the VIRTIO stack. |
| 167 | + * |
| 168 | + * @param dev Pointer to device structure. |
| 169 | + * @param vq_num Number of virtqueues the device uses. |
| 170 | + * @param vqs Array of pointers to vthe virtqueues used by the device. |
| 171 | + */ |
| 172 | +void virtio_mmio_register_device(struct virtio_device *vdev, int vq_num, struct virtqueue **vqs); |
| 173 | + |
| 174 | +/** |
| 175 | + * @brief Setup a virtqueue structure. |
| 176 | + * |
| 177 | + * @param dev Pointer to device structure. |
| 178 | + * @param idx Index of the virtqueue. |
| 179 | + * @param vq Pointer to virtqueue structure. |
| 180 | + * @param cb Pointer to virtqueue callback. Can be NULL. |
| 181 | + * @param cb_arg Argument for the virtqueue callback. |
| 182 | + * |
| 183 | + * @return pointer to virtqueue structure. |
| 184 | + */ |
| 185 | +struct virtqueue *virtio_mmio_setup_virtqueue(struct virtio_device *vdev, |
| 186 | + unsigned int idx, |
| 187 | + struct virtqueue *vq, |
| 188 | + void (*cb)(void *), |
| 189 | + void *cb_arg, |
| 190 | + const char *vq_name); |
| 191 | + |
| 192 | +/** |
| 193 | + * @brief VIRTIO MMIO device initialization. |
| 194 | + * |
| 195 | + * @param vmdev Pointer to virtio_mmio_device structure. |
| 196 | + * @param virt_mem_ptr Guest virtio (shared) memory base address (virtual). |
| 197 | + * @param cfg_mem_ptr Virtio device configuration memory base address (virtual). |
| 198 | + * @param user_data Pointer to custom user data. |
| 199 | + * |
| 200 | + * @return int 0 for success. |
| 201 | + */ |
| 202 | +int virtio_mmio_device_init(struct virtio_mmio_device *vmdev, uintptr_t virt_mem_ptr, |
| 203 | + uintptr_t cfg_mem_ptr, void *user_data); |
| 204 | + |
| 205 | +/** |
| 206 | + * @brief VIRTIO MMIO interrupt service routine. |
| 207 | + * |
| 208 | + * @param vdev Pointer to virtio_device structure. |
| 209 | + */ |
| 210 | +void virtio_mmio_isr(struct virtio_device *vdev); |
| 211 | + |
| 212 | +#ifdef __cplusplus |
| 213 | +} |
| 214 | +#endif |
| 215 | + |
| 216 | +#endif /* OPENAMP_VIRTIO_MMIO_H */ |
0 commit comments