From 6bc74e896c070f775d375c73f9cb94e3c84d4bfb Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Mon, 8 Aug 2022 22:55:28 +0300 Subject: [PATCH] allocate DMA frame --- ableos/src/arch/x86_64/mod.rs | 2 +- ableos/src/devices/pci/mod.rs | 3 ++ ableos/src/devices/pci/piix.rs | 80 ++++++++++++++++++++++++++++++++++ ableos/src/kmain.rs | 14 +++++- 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 ableos/src/devices/pci/piix.rs diff --git a/ableos/src/arch/x86_64/mod.rs b/ableos/src/arch/x86_64/mod.rs index ce008fa9..d2680fa0 100644 --- a/ableos/src/arch/x86_64/mod.rs +++ b/ableos/src/arch/x86_64/mod.rs @@ -34,7 +34,7 @@ pub fn start(boot_info: &'static BootInfo) -> ! { allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed"); - crate::kmain::kernel_main(); + crate::kmain::kernel_main(mapper, frame_allocator); } #[allow(unused)] diff --git a/ableos/src/devices/pci/mod.rs b/ableos/src/devices/pci/mod.rs index 5b7c1c45..c97b349e 100644 --- a/ableos/src/devices/pci/mod.rs +++ b/ableos/src/devices/pci/mod.rs @@ -19,6 +19,9 @@ pub use enums::*; pub mod port; use port::*; +// MassStorage_IDE (0x0101) +pub mod piix; + use self::devices::DeviceID; // PciDeviceInfo /////////////////////////////////////////////////////////////// diff --git a/ableos/src/devices/pci/piix.rs b/ableos/src/devices/pci/piix.rs new file mode 100644 index 00000000..416aa329 --- /dev/null +++ b/ableos/src/devices/pci/piix.rs @@ -0,0 +1,80 @@ +// FIXME: platform agnostic paging stuff +use x86_64::structures::paging::{PhysFrame, FrameAllocator, Size4KiB, mapper::MapToError, Mapper, Page}; +use x86_64::VirtAddr; + +use super::{PciDeviceInfo, check_device}; + +const PRDT_START: u64 = 0x_ffff_ffff_0000_0000; + +pub struct Piix { + device_info: PciDeviceInfo, + prdt_frame: Option, +} + +impl Piix { + pub fn new(bus: u8, device: u8) -> Option { + let device_info = check_device(bus, device)?; + trace!("device_info: {device_info}"); + + Some(Self { + device_info, + prdt_frame: None, + }) + } + + pub fn allocate_dma_frame( + &mut self, + mapper: &mut impl Mapper, + frame_allocator: &mut impl FrameAllocator, + ) -> Result<(), MapToError> { + use x86_64::structures::paging::PageTableFlags as Flags; + + let prdt_frame = frame_allocator.allocate_frame() + .ok_or(MapToError::FrameAllocationFailed)?; + let flags = Flags::NO_CACHE | Flags::PRESENT | Flags::WRITABLE; + + unsafe { + mapper.map_to( + Page::containing_address(VirtAddr::new(PRDT_START)), + prdt_frame, + flags, + frame_allocator, + )?.flush() + } + + trace!("{prdt_frame:#?}"); + self.prdt_frame = Some(prdt_frame); + Ok(()) + } + + pub fn read(&self) { + // bus master interface base address + let bmiba = self.device_info.bars[4] as *mut BusMasterInterface; + // bmiba.bmidtpp = prdt; + } +} + +#[derive(Copy, Clone, Debug)] +#[repr(C, packed)] +struct BusMasterInterface { + /// Bus Master IDE Command (primary) + pub bmicp: u8, + /// Reserved + pub _0: u8, + /// Bus Master IDE Status (primary) + pub bmisp: u8, + /// Reserved + pub _1: u8, + /// Bus Master IDE Descriptor Table Pointer (primary) + pub bmidtpp: u32, + /// Bus Master IDE Command (secondary) + pub bmics: u8, + /// Reserved + pub _2: u8, + /// Bus Master IDE Status (secondary) + pub bmiss: u8, + /// Reserved + pub _3: u8, + /// Bus Master IDE Descriptor Table Pointer (secondary) + pub bmidtps: u32, +} diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 0115208a..eea56b20 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -9,6 +9,7 @@ use core::sync::atomic::AtomicU64; use crate::arch::{drivers::sysinfo::master, init, sloop}; +use crate::devices::pci::piix::Piix; use crate::relib::network::socket::{SimpleSock, Socket}; use crate::{ boot_conf::KernelConfig, scratchpad, systeminfo::RELEASE_TYPE, TERM, @@ -17,12 +18,17 @@ use crate::{filesystem, hardware}; use kernel::KERNEL_VERSION; use spin::Lazy; +// FIXME: platform agnostic paging stuff +use x86_64::structures::paging::{Mapper, FrameAllocator, Size4KiB}; + // TODO: Change this structure to allow for multiple cores loaded pub static KERNEL_CONF: Lazy = Lazy::new(KernelConfig::new); /// The main entry point of the kernel -#[no_mangle] -pub fn kernel_main() -> ! { +pub fn kernel_main( + mut mapper: impl Mapper, + mut frame_allocator: impl FrameAllocator, +) -> ! { init::init(); // /* @@ -45,6 +51,10 @@ pub fn kernel_main() -> ! { filesystem::init().unwrap(); + // FIXME: unhardcode this and do device discovery + let mut piix = Piix::new(0, 3).unwrap(); + piix.allocate_dma_frame(&mut mapper, &mut frame_allocator).unwrap(); + /* // println!("abc");