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
#![feature(int_log)]

use std::env::args;

use pci_ids::FromId;
use twizzler_abi::{
    kso::{KactionCmd, KactionFlags},
    syscall::{ThreadSync, ThreadSyncReference, ThreadSyncWake},
};
use twizzler_driver::{
    bus::pcie::{PcieFunctionHeader, PcieKactionSpecific},
    device::{BusType, Device},
};
use twizzler_object::{ObjID, Object, ObjectInitFlags, Protections};

//mod nvme_test;

fn get_pcie_offset(bus: u8, device: u8, function: u8) -> usize {
    ((bus as usize * 256) + (device as usize * 8) + function as usize) * 4096
}

fn print_info(bus: u8, slot: u8, function: u8, cfg: &PcieFunctionHeader) -> Option<()> {
    if false {
        println!(
            "{} {} {}:: {:x} {:x} :: {:x} {:x} {:x}",
            bus,
            slot,
            function,
            cfg.vendor_id.get(),
            cfg.device_id.get(),
            cfg.class.get(),
            cfg.subclass.get(),
            cfg.progif.get(),
        );
    }
    let device = pci_ids::Device::from_vid_pid(cfg.vendor_id.get(), cfg.device_id.get())?;
    let vendor = device.vendor();
    let class = pci_ids::Class::from_id(cfg.class.get())?;
    //let subclass = pci_ids::Class::from_id(cfg.subclass.get())?;
    println!(
        "[devmgr] {:02x}:{:02x}.{:02x} {}: {} {}",
        bus,
        slot,
        function,
        class.name(),
        vendor.name(),
        device.name()
    );

    None
}

fn start_pcie_device(seg: &Device, bus: u8, device: u8, function: u8) {
    let kr = seg.kaction(
        KactionCmd::Specific(PcieKactionSpecific::RegisterDevice.into()),
        ((bus as u64) << 16) | ((device as u64) << 8) | (function as u64),
        KactionFlags::empty(),
        0,
    );
    match kr {
        Ok(_) => {}
        Err(_) => eprintln!(
            "failed to register pcie device {:x}.{:x}.{:x}",
            bus, device, function
        ),
    }
}

fn start_pcie(seg: Device) {
    println!("[devmgr] scanning PCIe bus");
    //let info = unsafe { bus.get_info::<PcieInfo>(0).unwrap() };
    let mmio = seg.get_mmio(0).unwrap();

    for bus in 0..=255 {
        for device in 0..32 {
            let off = get_pcie_offset(bus, device, 0);
            let cfg = unsafe { mmio.get_mmio_offset::<PcieFunctionHeader>(off) };
            if cfg.vendor_id.get() != 0xffff
                && cfg.device_id.get() != 0xffff
                && cfg.vendor_id.get() != 0
            {
                let mf = if cfg.header_type.get() & 0x80 != 0 {
                    7
                } else {
                    0
                };
                for function in 0..=mf {
                    let off = get_pcie_offset(bus, device, function);
                    let cfg = unsafe { mmio.get_mmio_offset::<PcieFunctionHeader>(off) };
                    if cfg.vendor_id.get() != 0xffff {
                        print_info(bus, device, function, cfg);
                        start_pcie_device(&seg, bus, device, function)
                    }
                }
            }
        }
    }
}

fn main() {
    println!("[devmgr] starting device manager {:?}", args());
    let id = args().into_iter().nth(1).unwrap().parse::<u128>().unwrap();
    let obj = Object::<std::sync::atomic::AtomicU64>::init_id(
        ObjID::new(id),
        Protections::WRITE | Protections::READ,
        ObjectInitFlags::empty(),
    )
    .unwrap();
    let device_root = twizzler_driver::get_bustree_root();
    for device in device_root.children() {
        if device.is_bus() && device.bus_type() == BusType::Pcie {
            start_pcie(device);
        }
    }

    //nvme_test::start();

    let base = unsafe { obj.base_unchecked() };
    base.store(1, std::sync::atomic::Ordering::SeqCst);
    twizzler_abi::syscall::sys_thread_sync(
        &mut [ThreadSync::new_wake(ThreadSyncWake::new(
            ThreadSyncReference::Virtual(base),
            usize::MAX,
        ))],
        None,
    )
    .unwrap();
}