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
use crate::ds::{cmd::PrpListOrBuffer, queue::subentry::Dptr, Address};

pub struct VirtualRegion<P: PhysicalPageCollection> {
    virt: *mut u8,
    _cache_type: CacheType,
    phys_page_list: P,
    len: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PrpMode {
    Single,
    Double,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DptrMode {
    Prp(PrpMode),
    Sgl,
}

pub trait PhysicalPageCollection {
    type DmaType;
    fn get_prp_list_or_buffer(
        &mut self,
        mode: PrpMode,
        dma: Self::DmaType,
    ) -> Option<PrpListOrBuffer>;

    fn get_dptr(&mut self, mode: DptrMode, dma: Self::DmaType) -> Option<Dptr> {
        if let DptrMode::Prp(prp_mode) = mode {
            let prp = self.get_prp_list_or_buffer(prp_mode, dma)?;
            match prp {
                PrpListOrBuffer::PrpList(address) => Some(Dptr::Prp(address, 0)),
                PrpListOrBuffer::Buffer(address) => Some(Dptr::Prp(address, 0)),
                PrpListOrBuffer::PrpFirstAndList(first, list) => Some(Dptr::Prp(first, list)),
            }
        } else {
            todo!()
        }
    }
}

pub enum CacheType {
    WriteBack,
    WriteThrough,
    Uncacheable,
}

impl PrpListOrBuffer {
    pub fn address(&self) -> Address {
        match self {
            PrpListOrBuffer::PrpList(a) => *a,
            PrpListOrBuffer::PrpFirstAndList(_, _) => panic!("cannot get single address"),
            PrpListOrBuffer::Buffer(a) => *a,
        }
    }

    pub fn is_list(&self) -> bool {
        match self {
            PrpListOrBuffer::PrpList(_) => true,
            PrpListOrBuffer::PrpFirstAndList(_, _) => true,
            PrpListOrBuffer::Buffer(_) => false,
        }
    }
}

impl<P: PhysicalPageCollection> VirtualRegion<P> {
    pub fn len(&self) -> usize {
        self.len
    }

    pub unsafe fn new(
        virt: *mut u8,
        len: usize,
        _cache_type: CacheType,
        phys_page_list: P,
    ) -> Self {
        Self {
            len,
            virt,
            _cache_type,
            phys_page_list,
        }
    }

    pub fn base<T>(&self) -> *const T {
        self.virt as *const T
    }

    pub fn base_mut<T>(&mut self) -> *mut T {
        self.virt as *mut T
    }

    pub fn get_prp_list_or_buffer<D>(
        &mut self,
        mode: PrpMode,
        dma: P::DmaType,
    ) -> Option<PrpListOrBuffer> {
        self.phys_page_list.get_prp_list_or_buffer(mode, dma)
    }

    pub fn get_dptr(&mut self, mode: DptrMode, dma: P::DmaType) -> Option<Dptr> {
        self.phys_page_list.get_dptr(mode, dma)
    }
}