1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Copyright (c) 2020 Alex Chi
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

//! virt-io driver

use crate::spinlock::{Mutex, MutexGuard};
use crate::{panic, info};
use crate::symbols::{PAGE_SIZE, PAGE_ORDER};
use crate::process::{wakeup, sleep};
use alloc::boxed::Box;
use crate::arch::__sync_synchronize;
use crate::uart::UART;
use core::sync::atomic::Ordering;

/// VIRTIO base address on QEMU RISC-V
pub const VIRTIO_MMIO_BASE: usize = 0x10001000;

/// VIRTIO MMIO address offset
#[allow(non_camel_case_types)]
pub enum VIRTIO_MMIO {
    MAGIC_VALUE = 0x0,
    VERSION = 0x4,
    DEVICE_ID = 0x8,
    VENDOR_ID = 0xc,
    DEVICE_FEATURES = 0x10,
    DRIVER_FEATURES = 0x20,
    GUEST_PAGE_SIZE = 0x28,
    QUEUE_SEL = 0x30,
    QUEUE_NUM_MAX = 0x34,
    QUEUE_NUM = 0x38,
    QUEUE_ALIGN = 0x3c,
    QUEUE_PFN = 0x40,
    QUEUE_READY = 0x44,
    QUEUE_NOTIFY = 0x50,
    INTERRUPT_STATUS = 0x60,
    INTERRUPT_ACK = 0x64,
    STATUS = 0x70,
}

impl VIRTIO_MMIO {
    /// Get address of MMIO from enum
    pub const fn val(self) -> usize {
        self as usize + VIRTIO_MMIO_BASE
    }
    /// Get pointer to MMIO register from enum
    pub const fn ptr(self) -> *mut u32 {
        self.val() as _
    }
}

#[allow(non_camel_case_types)]
pub enum VIRTIO_CONFIG_S {
    ACKNOWLDGE = 1,
    DRIVER = 1 << 1,
    DRIVER_OK = 1 << 2,
    FEATURES_OK = 1 << 3,
}

impl VIRTIO_CONFIG_S {
    pub const fn val(self) -> u32 { self as _ }
}

#[allow(non_camel_case_types)]
pub enum VIRTIO_FEATURE {
    BLK_F_RO = 5,
    BLK_F_SCSI = 7,
    BLK_F_CONFIG_WCE = 11,
    BLK_F_MQ = 12,
    F_ANY_LAYOUT = 27,
    RING_F_INDIRECT_DESC = 28,
    RING_F_EVENT_IDX = 29,
}

impl VIRTIO_FEATURE {
    pub fn bit(self) -> u32 {
        (1 << self as usize) as u32
    }
}

pub const DESC_NUM: usize = 8;

#[repr(C)]
pub struct VRingDesc {
    pub addr: usize,
    pub len: u32,
    pub flags: u16,
    pub next: u16,
}

impl VRingDesc {
    pub const fn new() -> Self {
        Self {
            addr: 0,
            len: 0,
            flags: 0,
            next: 0,
        }
    }
}

pub const VRING_DESC_F_NEXT: u16 = 1;
pub const VRING_DESC_F_WRITE: u16 = 2;

#[repr(C)]
pub struct VRingUsedElem {
    pub id: u32,
    pub len: u32,
}

impl VRingUsedElem {
    pub const fn new() -> Self {
        Self {
            id: 0,
            len: 0,
        }
    }
}

pub const VIRTIO_BLK_T_IN: u32 = 0;
pub const VIRTIO_BLK_T_OUT: u32 = 1;

#[repr(C)]
pub struct UsedArea {
    pub flags: u16,
    pub id: u16,
    pub elems: [VRingUsedElem; DESC_NUM],
}

impl UsedArea {
    pub const fn new() -> Self {
        Self {
            flags: 0,
            id: 0,
            elems: [VRingUsedElem::new(); DESC_NUM],
        }
    }
}

pub struct InflightOp {
    pub buf: Box<Buf>,
    pub status: u8,
}

/// Size of avail array
const AVAIL_SZ: usize = (PAGE_SIZE - DESC_NUM * core::mem::size_of::<VRingDesc>()) / core::mem::size_of::<u16>();

#[repr(C)]
#[repr(align(4096))]
pub struct VirtIOData {
    /// VIRTIO MMIO descriptor register
    pub desc: [VRingDesc; DESC_NUM],
    /// VIRTIO MMIO descriptor avail register (padding to page size)
    pub avail: [u16; AVAIL_SZ],
    /// VIRTIO MMIO descriptor used register
    pub used: [UsedArea; DESC_NUM],

    /// is descriptor free
    pub free: [bool; DESC_NUM],
    /// used index of used array
    pub used_idx: u16,
    /// in-flight operations
    pub info: [Option<InflightOp>; DESC_NUM],
}

pub struct VirtIO(Mutex<VirtIOData>);

/// VIRTIO buffer size
pub const BSIZE: usize = 1024;

/// VIRTIO Buffer
#[repr(C)]
pub struct Buf {
    pub valid: bool,
    /// TODO: this can be removed
    pub disk: i32,
    /// device ID
    pub dev: u32,
    /// block number
    pub blockno: u32,
    /// buffer data
    pub data: [u8; BSIZE],
}

impl Buf {
    pub const fn new() -> Self {
        Self {
            valid: false,
            disk: 0,
            dev: 0,
            blockno: 0,
            data: [0; BSIZE],
        }
    }
}

#[repr(C)]
pub struct BlkOutHdr {
    pub blk_type: u32,
    reserved: u32,
    sector: usize,
}

impl VirtIOData {
    /// Free one descriptor
    fn free_desc(&mut self, i: usize) {
        if i >= DESC_NUM {
            panic!("invalid desc");
        }
        if self.free[i] {
            panic!("already free");
        }
        self.desc[i].addr = 0;
        self.free[i] = true;
        wakeup(&self.free[0]);
    }

    /// Allocate one descriptor
    fn alloc_desc(&mut self) -> Option<usize> {
        for i in 0..DESC_NUM {
            if self.free[i] {
                self.free[i] = false;
                return Some(i);
            }
        }
        None
    }

    /// Allocate three descriptors, return array of indices
    fn alloc3_desc(&mut self) -> Option<[usize; 3]> {
        let mut idx = [0; 3];
        for i in 0..3 {
            match self.alloc_desc() {
                Some(x) => idx[i] = x,
                None => {
                    for j in 0..i {
                        self.free_desc(idx[j]);
                    }
                    return None;
                }
            }
        }
        Some(idx)
    }

    /// Free descriptor chain
    fn free_chain(&mut self, mut i: usize) {
        loop {
            self.free_desc(i);
            if self.desc[i].flags & VRING_DESC_F_NEXT != 0 {
                i = self.desc[i].next as usize;
            } else {
                break;
            }
        }
    }
}

impl VirtIO {
    pub const fn new() -> Self {
        Self(
            Mutex::new(VirtIOData {
                desc: [VRingDesc::new(); DESC_NUM],
                avail: [0; AVAIL_SZ],
                used: [UsedArea::new(); DESC_NUM],
                free: [false; DESC_NUM],
                used_idx: 0,
                info: [None; DESC_NUM],
            }, "vdisk"))
    }

    /// Initialize VIRTIO driver.
    ///
    /// Should be called in booting hart.
    pub unsafe fn init(&mut self) {
        use VIRTIO_MMIO::*;
        use VIRTIO_CONFIG_S::*;
        use VIRTIO_FEATURE::*;

        let mut vio = self.0.get();

        if MAGIC_VALUE.ptr().read_volatile() != 0x74726976 {
            panic!("cannot find virtio disk: magic value");
        }
        if VERSION.ptr().read_volatile() != 1 {
            panic!("cannot find virtio disk: version");
        }
        if DEVICE_ID.ptr().read_volatile() != 2 {
            panic!("cannot find virtio disk: device id");
        }
        if VENDOR_ID.ptr().read_volatile() != 0x554d4551 {
            panic!("cannot find virtio disk: vendor id");
        }

        let mut status: u32 = 0;
        status |= ACKNOWLDGE.val();
        STATUS.ptr().write_volatile(status);

        status |= DRIVER.val();
        STATUS.ptr().write_volatile(status);

        let mut features: u32 = DEVICE_FEATURES.ptr().read_volatile();
        features &= !BLK_F_RO.bit();
        features &= !BLK_F_SCSI.bit();
        features &= !BLK_F_CONFIG_WCE.bit();
        features &= !BLK_F_MQ.bit();
        features &= !F_ANY_LAYOUT.bit();
        features &= !RING_F_EVENT_IDX.bit();
        features &= !RING_F_INDIRECT_DESC.bit();
        DEVICE_FEATURES.ptr().write_volatile(features);

        status |= FEATURES_OK.val();
        STATUS.ptr().write_volatile(status);

        status |= DRIVER_OK.val();
        STATUS.ptr().write_volatile(status);

        GUEST_PAGE_SIZE.ptr().write_volatile(PAGE_SIZE as u32);

        QUEUE_SEL.ptr().write_volatile(0);
        let max = QUEUE_NUM_MAX.ptr().read_volatile();
        if max == 0 {
            panic!("virtio disk has no queue");
        }
        if max < DESC_NUM as u32 {
            panic!("virtio disk max queue too short {} < {}", max, DESC_NUM);
        }
        QUEUE_NUM.ptr().write_volatile(DESC_NUM as u32);

        QUEUE_PFN.ptr().write_volatile(((vio as *mut _ as usize) >> PAGE_ORDER) as u32);

        for i in 0..DESC_NUM {
            vio.free[i] = true;
        }
    }

    /// Read-write operation
    fn rw(&mut self, mut b: Box<Buf>, write: bool) -> Box<Buf> {
        use VIRTIO_MMIO::*;

        let sector = b.blockno as usize * (BSIZE / 512);

        let mut vio = self.0.lock();

        let idx: [usize; 3] = loop {
            if let Some(idx) = vio.alloc3_desc() {
                break idx;
            }
            vio = sleep(&vio.free[0] as *const _, vio);
        };

        // info!("3 desc: {:?}", idx);

        let buf0 = BlkOutHdr {
            reserved: 0,
            sector,
            blk_type: if write { VIRTIO_BLK_T_OUT } else { VIRTIO_BLK_T_IN },
        };


        // VIRTIO 5.2.6.4
        // MUST use a single 8-byte descriptor containing type, reserved and sector,
        // followed by descriptors for data, then finally a separate 1-byte descriptor for status.

        {
            let desc0 = &mut vio.desc[idx[0]];
            desc0.addr = &buf0 as *const _ as usize;
            desc0.len = core::mem::size_of::<BlkOutHdr>() as u32;
            desc0.flags = VRING_DESC_F_NEXT;
            desc0.next = idx[1] as u16;
        }

        {
            let desc1 = &mut vio.desc[idx[1]];
            desc1.addr = b.data.as_mut_ptr() as usize;
            desc1.len = BSIZE as u32;
            desc1.flags = if write { 0 } else { VRING_DESC_F_WRITE };
            desc1.flags |= VRING_DESC_F_NEXT;
            desc1.next = idx[2] as u16;
        }

        b.disk = 1;
        vio.info[idx[0]] = Some(InflightOp {
            buf: b,
            status: 0,
        });

        {
            let addr = &vio.info[idx[0]].as_ref().unwrap().status as *const _ as usize;
            {
                let desc2 = &mut vio.desc[idx[2]];

                desc2.addr = addr;
                desc2.len = 1;
                desc2.flags = VRING_DESC_F_WRITE;
                desc2.next = 0;
            }

            let idx_id = 2 + vio.avail[1] as usize % DESC_NUM;
            vio.avail[idx_id] = idx[0] as u16;

            __sync_synchronize();

            vio.avail[1] = vio.avail[1] + 1;

            unsafe { QUEUE_NOTIFY.ptr().write_volatile(0); }
            // info!("{:x}", &vio.info[idx[0]].as_ref().unwrap().status as *const _ as usize);
            let buf_addr = &*vio.info[idx[0]].as_ref().unwrap().buf as *const _;
            // info!("sleep {:x} id={}", buf_addr as usize, vio.info[idx[0]].as_ref().unwrap().status);
            while vio.info[idx[0]].as_ref().unwrap().buf.disk == 1 {
                vio = sleep(buf_addr, vio);
            }
        }
        let result = core::mem::replace(&mut vio.info[idx[0]], None);
        unsafe { vio.info[idx[0]] = core::mem::zeroed(); }
        vio.info[idx[0]] = None;
        vio.free_chain(idx[0]);
        result.unwrap().buf
    }

    /// Read from device and block number
    pub fn read(&mut self, dev: u32, blockno: u32) -> Box<Buf> {
        let mut buf = box Buf::new();
        buf.dev = dev;
        buf.blockno = blockno;
        self.rw(buf, false)
    }

    /// Write buffer to disk
    pub fn write(&mut self, buf: Box<Buf>) {
        self.rw(buf, true);
    }
}

/// VirtIO driver object
static mut __VIRTIO: VirtIO = VirtIO::new();

/// Global function to get an instance of VirtIO driver
#[allow(non_snake_case)]
pub fn VIRTIO() -> &'static mut VirtIO { unsafe { &mut __VIRTIO } }

pub unsafe fn init() {
    VIRTIO().init();
}


/// VIRTIO interrupt
pub fn virtiointr() {
    use crate::info;
    let mut disk = VIRTIO().0.lock();
    while disk.used_idx as usize % DESC_NUM != disk.used[0].id as usize % DESC_NUM {

        let id = disk.used[0].elems[disk.used_idx as usize].id as usize;

        if disk.info[id].is_none() {
            panic!("invalid id");
        }

        let info = disk.info[id].as_mut().unwrap();

        // info!("processing 0x{:x}", &*info.buf as *const _ as usize);

        if info.status != 0 {
            panic!("virtio_disk_intr status status={} id={}", info.status, id);
        }

        info.buf.disk = 0;

        wakeup(&*info.buf);

        disk.used_idx = ((disk.used_idx + 1) as usize % DESC_NUM) as u16;
    }
}

pub mod tests {
    use super::*;

    pub fn tests() -> &'static [(&'static str, fn())] {
        &[
            ("memory layout", test_memory_layout),
            ("read and write", test_rw)
        ]
    }

    /// Test virtio memory layout
    pub fn test_memory_layout() {
        let virtio = VIRTIO().0.lock();
        assert_eq!(&virtio.desc as *const _ as usize % PAGE_SIZE, 0);
        assert_eq!(&virtio.used as *const _ as usize % PAGE_SIZE, 0);
        assert_eq!(&virtio.used as *const _ as usize - &virtio.desc as *const _ as usize, PAGE_SIZE);
    }

    use crate::{print, println};

    /// Test read and write
    pub fn test_rw() {
        let virtio = VIRTIO();
        let b = virtio.read(1, 0);
        unsafe { println!("size: {}", core::ptr::read(b.data.as_ptr() as *const usize)); }
        unsafe { println!("offset: {}", core::ptr::read(b.data.as_ptr().add(8) as *const usize)); }
        for i in 16..b.data.len() {
            let d = b.data[i];
            if d == 0 {
                break;
            }
            print!("{}", b.data[i] as char);
        }
        println!();
    }
}