OpenAMP Library  353
virtio_mmio.h
Go to the documentation of this file.
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 
134  void *base;
135 
137  size_t size;
138 };
139 
143  struct virtio_device vdev;
144 
146  struct metal_io_region cfg_io;
147 
149  struct metal_io_region shm_io;
150 
153 
156 
158  unsigned int device_mode;
159 
161  unsigned int irq;
162 
164  void *user_data;
165 };
166 
174 void virtio_mmio_register_device(struct virtio_device *vdev, int vq_num, struct virtqueue **vqs);
175 
189  unsigned int idx,
190  struct virtqueue *vq,
191  void (*cb)(void *),
192  void *cb_arg,
193  const char *vq_name);
194 
205 int virtio_mmio_device_init(struct virtio_mmio_device *vmdev, uintptr_t virt_mem_ptr,
206  uintptr_t cfg_mem_ptr, void *user_data);
207 
213 void virtio_mmio_isr(struct virtio_device *vdev);
214 
215 #ifdef __cplusplus
216 }
217 #endif
218 
219 #endif /* OPENAMP_VIRTIO_MMIO_H */
Structure definition for virtio devices for use by the applications/drivers.
Definition: virtio.h:188
VIRTIO MMIO memory area.
Definition: virtio_mmio.h:132
void * base
Memory region physical address.
Definition: virtio_mmio.h:134
size_t size
Memory region size.
Definition: virtio_mmio.h:137
A VIRTIO MMIO device.
Definition: virtio_mmio.h:141
unsigned int irq
Interrupt number.
Definition: virtio_mmio.h:161
struct virtio_mmio_dev_mem shm_mem
VIRTIO device pre-shared memory.
Definition: virtio_mmio.h:155
struct virtio_mmio_dev_mem cfg_mem
VIRTIO device configuration space.
Definition: virtio_mmio.h:152
unsigned int device_mode
VIRTIO_DEV_DRIVER or VIRTIO_DEV_DEVICE.
Definition: virtio_mmio.h:158
struct virtio_device vdev
Base virtio device structure.
Definition: virtio_mmio.h:143
struct metal_io_region shm_io
Pre-shared memory space metal_io_region.
Definition: virtio_mmio.h:149
struct metal_io_region cfg_io
Device configuration space metal_io_region.
Definition: virtio_mmio.h:146
void * user_data
Custom user data.
Definition: virtio_mmio.h:164
Local virtio queue to manage a virtio ring for sending or receiving.
Definition: virtqueue.h:78
const char * vq_name
Name of the virtio queue.
Definition: virtqueue.h:83
void virtio_mmio_register_device(struct virtio_device *vdev, int vq_num, struct virtqueue **vqs)
Register a VIRTIO device with the VIRTIO stack.
Definition: virtio_mmio_drv.c:216
struct virtqueue * virtio_mmio_setup_virtqueue(struct virtio_device *vdev, unsigned int idx, struct virtqueue *vq, void(*cb)(void *), void *cb_arg, const char *vq_name)
Setup a virtqueue structure.
Definition: virtio_mmio_drv.c:228
int virtio_mmio_device_init(struct virtio_mmio_device *vmdev, uintptr_t virt_mem_ptr, uintptr_t cfg_mem_ptr, void *user_data)
VIRTIO MMIO device initialization.
Definition: virtio_mmio_drv.c:170
void virtio_mmio_isr(struct virtio_device *vdev)
VIRTIO MMIO interrupt service routine.
Definition: virtio_mmio_drv.c:310