550W Document
550W, a high-end OS
virtio.h
浏览该文件的文档.
1 #pragma once
2 
3 #include <asm/pgtable.h>
4 #include <common/types.h>
5 #include <os/lock.h>
6 
7 #define VIRTIO0 0x10001000
8 #define VIRTIO0_IRQ 1
9 
10 #define VIRTIO_MAGIC 0x74726976
11 #define VIRTIO_VERSION 0x1
12 #define VIRTIO_DEVICE_ID_INV 0x0
13 #define VIRTIO_DEVICE_ID_NET 0x1
14 #define VIRTIO_DEVICE_ID_BLK 0x2
15 #define VIRTIO_VENDOR_ID_QEMU 0x554d4551
16 
17 #define VIRTIO_MMIO_MAGIC_VALUE 0x000 // 0x74726976
18 #define VIRTIO_MMIO_VERSION 0x004 // version; should be 2
19 #define VIRTIO_MMIO_DEVICE_ID 0x008 // device type; 1 is net, 2 is disk
20 #define VIRTIO_MMIO_VENDOR_ID 0x00c // 0x554d4551
21 #define VIRTIO_MMIO_DEVICE_FEATURES 0x010
22 #define VIRTIO_MMIO_DRIVER_FEATURES 0x020
23 #define VIRTIO_MMIO_GUEST_PAGE_SIZE 0x028 // page size for PFN, write-only
24 #define VIRTIO_MMIO_QUEUE_SEL 0x030 // select queue, write-only
25 #define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034 // max size of current queue, read-only
26 #define VIRTIO_MMIO_QUEUE_NUM 0x038 // size of current queue, write-only
27 #define VIRTIO_MMIO_QUEUE_PFN 0x040 // physical page number for queue, read/write
28 #define VIRTIO_MMIO_QUEUE_READY 0x044 // ready bit
29 #define VIRTIO_MMIO_QUEUE_NOTIFY 0x050 // write-only
30 #define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 // read-only
31 #define VIRTIO_MMIO_INTERRUPT_ACK 0x064 // write-only
32 #define VIRTIO_MMIO_STATUS 0x070 // read/write
33 #define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 // physical address for descriptor table, write-only
34 #define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084
35 #define VIRTIO_MMIO_DRIVER_DESC_LOW 0x090 // physical address for available ring, write-only
36 #define VIRTIO_MMIO_DRIVER_DESC_HIGH 0x094
37 #define VIRTIO_MMIO_DEVICE_DESC_LOW 0x0a0 // physical address for used ring, write-only
38 #define VIRTIO_MMIO_DEVICE_DESC_HIGH 0x0a4
39 
40 // status register bits, from qemu virtio_config.h
41 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
42 #define VIRTIO_CONFIG_S_DRIVER 2
43 #define VIRTIO_CONFIG_S_DRIVER_OK 4
44 #define VIRTIO_CONFIG_S_FEATURES_OK 8
45 
46 // device feature bits
47 #define VIRTIO_BLK_F_RO 5 /* Disk is read-only */
48 #define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */
49 #define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */
50 #define VIRTIO_BLK_F_MQ 12 /* support more than one vq */
51 
52 #define VIRTIO_BLK_T_IN 0 // read the disk
53 #define VIRTIO_BLK_T_OUT 1 // write the disk
54 
55 #define VIRTIO_F_ANY_LAYOUT 27
56 #define VIRTIO_RING_F_INDIRECT_DESC 28
57 #define VIRTIO_RING_F_EVENT_IDX 29
58 
59 #define VRING_DESC_F_NEXT 1 // chained with another descriptor
60 #define VRING_DESC_F_WRITE 2 // device writes (vs read
61 
62 // this many virtio descriptors.
63 // must be a power of two.
64 #define DESC_NUM 8
65 #define BSIZE 512
66 #define BNUM 1600
67 
68 #define MAXOPBLOCKS 10 // max # of blocks any FS op writes
69 #define LOGSIZE (MAXOPBLOCKS * 3) // max data blocks in on-disk log
70 #define NBUF (MAXOPBLOCKS * 3) // size of disk block cache
71 
72 #define DEV_VDA2 0
73 
74 extern uintptr_t virtio_base;
75 
76 #define R(r) ((volatile uint32 *)(virtio_base + (r)))
77 
78 typedef struct buf {
79  int valid;
80  int disk; // does disk "own" buf?
82  uint sectorno; // sector number
85  struct buf *prev;
86  struct buf *next;
87  char data[BSIZE];
89 
90 typedef struct vring_desc_t {
96 
97 typedef struct vring_used_elem {
98  uint32 id; // index of start of completed descriptor chain
101 
102 typedef struct vring_used_area {
107 
108 typedef struct disk {
109  // memory for virtio descriptors &c for queue 0.
110  // this is a global instead of allocated because it must
111  // be multiple contiguous pages, which kalloc()
112  // doesn't support, and page aligned.
117 
118  // our own book-keeping.
119  char free[DESC_NUM]; // is a descriptor free?
120  uint16 used_idx; // we've looked this far in used[2..NUM].
121 
122  // track info about in-flight operations,
123  // for use when completion interrupt arrives.
124  // indexed by first descriptor index of chain.
125  struct {
126  struct buf *b;
127  char status;
129 
131 
133 
134 extern disk_t disk;
135 
137 void d_virtio_disk_rw(struct buf *b, int write);
139 
140 void d_binit(void);
141 struct buf *d_bread(uint, uint);
142 void d_brelse(struct buf *);
143 void d_bwrite(struct buf *);
144 
145 void d_sd_read(char *buffers, uint *start_block_id, uint block_num);
146 void d_sd_write(char *buffers, uint *start_block_ids, uint block_num);
#define NORMAL_PAGE_SIZE
Definition: pgtable.h:13
Definition: virtio.h:78
int disk
Definition: virtio.h:80
uint sectorno
Definition: virtio.h:82
char data[BSIZE]
Definition: virtio.h:87
sleep_lock_t lock
Definition: virtio.h:83
int valid
Definition: virtio.h:79
struct buf * prev
Definition: virtio.h:85
uint refcnt
Definition: virtio.h:84
struct buf * next
Definition: virtio.h:86
uint dev
Definition: virtio.h:81
Definition: virtio.h:108
vring_used_area_t * used
Definition: virtio.h:116
struct buf * b
Definition: virtio.h:126
uint16 * avail
Definition: virtio.h:115
char free[DESC_NUM]
Definition: virtio.h:119
uint16 used_idx
Definition: virtio.h:120
spin_lock_t vdisk_lock
Definition: virtio.h:130
char pages[2 *NORMAL_PAGE_SIZE]
Definition: virtio.h:113
vring_desc_t * desc
Definition: virtio.h:114
struct disk::@0 info[DESC_NUM]
char status
Definition: virtio.h:127
Definition: lock.h:33
Definition: lock.h:17
Definition: virtio.h:90
uint16 flags
Definition: virtio.h:93
uint32 len
Definition: virtio.h:92
uint16 next
Definition: virtio.h:94
uint64 addr
Definition: virtio.h:91
Definition: virtio.h:102
uint16 id
Definition: virtio.h:104
vring_used_elem_t elems[DESC_NUM]
Definition: virtio.h:105
uint16 flags
Definition: virtio.h:103
Definition: virtio.h:97
uint32 id
Definition: virtio.h:98
uint32 len
Definition: virtio.h:99
unsigned short uint16
Definition: types.h:35
unsigned int uint32
Definition: types.h:37
unsigned long long uint64
Definition: types.h:39
unsigned int uint
Definition: types.h:40
uint64 uintptr_t
Definition: types.h:47
struct buf * d_bread(uint, uint)
Definition: virtio_blk.c:274
uintptr_t virtio_base
void d_sd_read(char *buffers, uint *start_block_id, uint block_num)
Definition: virtio_blk.c:330
void d_bwrite(struct buf *)
Definition: virtio_blk.c:287
void d_sd_write(char *buffers, uint *start_block_ids, uint block_num)
Definition: virtio_blk.c:339
#define BSIZE
Definition: virtio.h:65
#define DESC_NUM
Definition: virtio.h:64
struct vring_used_elem vring_used_elem_t
struct vring_used_area vring_used_area_t
void d_virtio_disk_rw(struct buf *b, int write)
Definition: virtio_blk.c:118
struct vring_desc_t vring_desc_t
struct disk disk_t
void d_binit(void)
Definition: virtio_blk.c:218
void d_virtio_disk_intr(void)
struct buf buf_t
void d_virtio_disk_init(void)
void d_brelse(struct buf *)
Definition: virtio_blk.c:296
disk_t disk