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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! BAR Management.

use crate::spec::cfg_space;
use inspect::Inspect;

/// A parsed BAR mapping.
#[derive(Debug, Inspect)]
pub struct BarMapping {
    /// Associated BAR register index
    pub index: u8,
    /// Base address of the mapping
    pub base_address: u64,
    /// Length of the mapping
    pub len: u64,
}

/// A set of parsed BAR mappings.
#[derive(Debug, Default)]
pub struct BarMappings(Vec<BarMapping>);

impl Inspect for BarMappings {
    fn inspect(&self, req: inspect::Request<'_>) {
        let mut res = req.respond();
        for bar in self.0.iter() {
            res.field(
                &format!("bar{}", bar.index),
                format!("{:#010x?}:{:#x?}", bar.base_address, bar.len),
            );
        }
    }
}

impl BarMappings {
    /// Parses a set of BARs into mappings.
    pub fn parse(base_addresses: &[u32; 6], bar_masks: &[u32; 6]) -> Self {
        let mut mappings = Vec::new();
        let mut i = 0;
        while i < base_addresses.len() {
            let bar_address;
            let mut bar_mask;
            let len;
            if bar_masks[i] & cfg_space::BarEncodingBits::TYPE_64_BIT.bits() != 0 {
                bar_mask = (bar_masks[i + 1] as u64) << 32 | bar_masks[i] as u64;
                bar_address = (base_addresses[i + 1] as u64) << 32 | base_addresses[i] as u64;
                len = 2;
            } else {
                bar_mask = bar_masks[i] as i32 as i64 as u64; // sign extend
                bar_address = base_addresses[i] as u64;
                len = 1;
            };
            bar_mask &= !0xf;
            if bar_mask != 0 {
                mappings.push(BarMapping {
                    index: i as u8,
                    base_address: bar_address & bar_mask,
                    len: !bar_mask + 1,
                });
            }
            i += len;
        }
        Self(mappings)
    }

    /// Finds a BAR + offset by address.
    pub fn find(&self, address: u64) -> Option<(u8, u16)> {
        for bar_mapping in self.0.iter() {
            if address >= bar_mapping.base_address
                && address - bar_mapping.base_address < bar_mapping.len
            {
                return Some((
                    bar_mapping.index,
                    (address - bar_mapping.base_address).try_into().unwrap(),
                ));
            }
        }
        None
    }

    /// Gets the base address configured for `bar`.
    pub fn get(&self, bar: u8) -> Option<u64> {
        for bar_mapping in self.0.iter() {
            if bar_mapping.index == bar {
                return Some(bar_mapping.base_address);
            }
        }
        None
    }

    /// Returns an iterator through the mappings.
    pub fn iter(&self) -> impl Iterator<Item = &BarMapping> {
        self.0.iter()
    }
}