Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions kernel/src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub mod x86_64;
#[cfg(target_arch = "x86_64")]
pub use self::x86_64::pci::pci::X86_64PciArch as PciArch;
#[cfg(target_arch = "x86_64")]
pub use self::x86_64::*; //公开x86_64架构下的函数,使外界接口统一
use crate::driver::pci::pci::{BusDeviceFunction, PciError, PciRoot, SegmentGroupNumber};
/// TraitPciArch Pci架构相关函数,任何架构都应独立实现trait里的函数
pub trait TraitPciArch {
/// @brief 读取寄存器值,x86_64架构通过读取两个特定io端口实现
/// @param bus_device_function 设备的唯一标识符
/// @param offset 寄存器偏移值
/// @return 读取到的值
fn read_config(bus_device_function: &BusDeviceFunction, offset: u8) -> u32;
/// @brief 写入寄存器值,x86_64架构通过读取两个特定io端口实现
/// @param bus_device_function 设备的唯一标识符
/// @param offset 寄存器偏移值
/// @param data 要写入的值
fn write_config(bus_device_function: &BusDeviceFunction, offset: u8, data: u32);
/// @brief PCI域地址到存储器域地址的转换,x86_64架构为一一对应
/// @param address PCI域地址
/// @return Result<usize, PciError> 转换结果或出错原因
fn address_pci_to_address_memory(address: usize) -> Result<usize, PciError>;
/// @brief 获取Segement的root地址,x86_64架构为acpi mcfg表中读取
/// @param segement 组id
/// @return Result<PciRoot, PciError> 转换结果或出错原因
fn ecam_root(segement: SegmentGroupNumber) -> Result<PciRoot, PciError>;
}
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::include::bindings::bindings::{process_control_block, switch_proc};

use core::sync::atomic::compiler_fence;

use super::fpu::{fp_state_save, fp_state_restore};
use super::fpu::{fp_state_restore, fp_state_save};

/// @brief 切换进程的上下文(没有切换页表的动作)
///
Expand Down
3 changes: 2 additions & 1 deletion kernel/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pub mod asm;
pub mod context;
pub mod cpu;
pub mod fpu;
pub mod interrupt;
pub mod mm;
pub mod pci;
pub mod sched;
pub mod fpu;
1 change: 1 addition & 0 deletions kernel/src/arch/x86_64/pci/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod pci;
70 changes: 70 additions & 0 deletions kernel/src/arch/x86_64/pci/pci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::arch::TraitPciArch;
use crate::driver::acpi::acpi::mcfg_find_segment;
use crate::driver::pci::pci::{
BusDeviceFunction, PciError, PciRoot, SegmentGroupNumber, PORT_PCI_CONFIG_ADDRESS,
PORT_PCI_CONFIG_DATA,
};
use crate::include::bindings::bindings::{
acpi_get_MCFG, acpi_iter_SDT, acpi_system_description_table_header_t, io_in32, io_out32,
};

use core::ffi::c_void;
use core::ptr::NonNull;
pub struct X86_64PciArch {}
impl TraitPciArch for X86_64PciArch {
fn read_config(bus_device_function: &BusDeviceFunction, offset: u8) -> u32 {
// 构造pci配置空间地址
let address = ((bus_device_function.bus as u32) << 16)
| ((bus_device_function.device as u32) << 11)
| ((bus_device_function.function as u32 & 7) << 8)
| (offset & 0xfc) as u32
| (0x80000000);
let ret = unsafe {
io_out32(PORT_PCI_CONFIG_ADDRESS, address);
let temp = io_in32(PORT_PCI_CONFIG_DATA);
temp
};
return ret;
}

fn write_config(bus_device_function: &BusDeviceFunction, offset: u8, data: u32) {
let address = ((bus_device_function.bus as u32) << 16)
| ((bus_device_function.device as u32) << 11)
| ((bus_device_function.function as u32 & 7) << 8)
| (offset & 0xfc) as u32
| (0x80000000);
unsafe {
io_out32(PORT_PCI_CONFIG_ADDRESS, address);
// 写入数据
io_out32(PORT_PCI_CONFIG_DATA, data);
}
}

fn address_pci_to_address_memory(address: usize) -> Result<usize, PciError> {
Ok(address)
}

fn ecam_root(segement: SegmentGroupNumber) -> Result<PciRoot, PciError> {
let mut data: usize = 0;
let data_point = &mut data;
unsafe {
acpi_iter_SDT(Some(acpi_get_MCFG), data_point as *mut usize as *mut c_void);
};
//kdebug!("{}",data);
//loop{}
let head = NonNull::new(data as *mut acpi_system_description_table_header_t).unwrap();
let outcome = unsafe { mcfg_find_segment(head).as_ref() };
for segmentgroupconfiguration in outcome {
if segmentgroupconfiguration.segement_group_number == segement {
return Ok(PciRoot {
physical_address_base: segmentgroupconfiguration.base_address,
mmio_base: None,
segement_group_number: segement,
bus_begin: segmentgroupconfiguration.bus_begin,
bus_end: segmentgroupconfiguration.bus_end,
});
}
}
return Err(PciError::SegmentNotFound);
}
}
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion kernel/src/driver/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

CFLAGS += -I .

kernel_driver_subdirs:=video interrupt usb pci acpi disk keyboard mouse multiboot2 timers hid virtio
kernel_driver_subdirs:=video interrupt usb pci acpi disk keyboard mouse multiboot2 timers hid

ECHO:
@echo "$@"
Expand Down
16 changes: 16 additions & 0 deletions kernel/src/driver/acpi/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ bool acpi_get_HPET(const struct acpi_system_description_table_header_t *_iter_da
return true;
}

/**
* @brief 获取MCFG MCFG_description_table
*
* @param _iter_data 要被迭代的信息的结构体
* @param _data 返回的MCFG表的虚拟地址
* @return true
* @return false
*/
bool acpi_get_MCFG(const struct acpi_system_description_table_header_t *_iter_data, void *_data)
{
if (!(_iter_data->Signature[0] == 'M' && _iter_data->Signature[1] == 'C' && _iter_data->Signature[2] == 'F' && _iter_data->Signature[3] == 'G'))
return false;
*(ul *)_data = (ul)_iter_data;
return true;
}

/**
* @brief 初始化acpi模块
*
Expand Down
9 changes: 9 additions & 0 deletions kernel/src/driver/acpi/acpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,14 @@ bool acpi_get_MADT(const struct acpi_system_description_table_header_t *_iter_da
*/
bool acpi_get_HPET(const struct acpi_system_description_table_header_t *_iter_data, void *_data);

/**
* @brief 获取MCFG MCFG_description_table
*
* @param _iter_data 要被迭代的信息的结构体
* @param _data 返回的MCFG表的虚拟地址
* @return true
* @return false
*/
bool acpi_get_MCFG(const struct acpi_system_description_table_header_t *_iter_data, void *_data);
// 初始化acpi模块
void acpi_init();
27 changes: 27 additions & 0 deletions kernel/src/driver/acpi/acpi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::driver::pci::pci::SegmentGroupNumber;
use crate::include::bindings::bindings::acpi_system_description_table_header_t;
use core::ptr::{slice_from_raw_parts_mut, NonNull};
// MCFG表中的Segement配置部分,开始位置为44+16*n
#[repr(C, packed)]
pub struct Segement_Configuration_Space {
pub base_address: u64,
pub segement_group_number: SegmentGroupNumber,
pub bus_begin: u8,
pub bus_end: u8,
pub reverse: u32,
}

/// @brief 获取Segement_Configuration_Space的数量并返回对应数量的Segement_Configuration_Space的切片指针
/// @param head acpi_system_description_table_header_t的指针
/// @return NonNull<[Segement_Configuration_Space]>
pub fn mcfg_find_segment(
head: NonNull<acpi_system_description_table_header_t>,
) -> NonNull<[Segement_Configuration_Space]> {
let table_length = unsafe { (*head.as_ptr()).Length };
let number_of_segments = ((table_length - 44) / 16) as u16;
NonNull::new(slice_from_raw_parts_mut(
(head.as_ptr() as usize + 44) as *mut _,
number_of_segments as usize,
))
.unwrap()
}
1 change: 1 addition & 0 deletions kernel/src/driver/acpi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod acpi;
16 changes: 4 additions & 12 deletions kernel/src/driver/base/device/bus.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
use alloc::{
collections::BTreeMap,
sync::Arc
};
use lazy_static::lazy_static;
use core::fmt::Debug;
use super::{driver::Driver, Device, DeviceState, IdTable};
use crate::libs::spinlock::SpinLock;
use super::{
driver::Driver,
DeviceState,
IdTable,
Device
};
use alloc::{collections::BTreeMap, sync::Arc};
use core::fmt::Debug;
use lazy_static::lazy_static;

/// @brief: 总线状态
#[derive(Debug, Copy, Clone)]
Expand Down
4 changes: 1 addition & 3 deletions kernel/src/driver/base/device/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use core::{any::Any, fmt::Debug};

/// @brief: Driver error
#[allow(dead_code)]
#[derive(Debug)]
#[derive(PartialEq, Eq)]
#[derive(Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DriverError {
ProbeError,
}
Expand Down
9 changes: 2 additions & 7 deletions kernel/src/driver/base/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ pub enum DeviceType {
}

/// @brief: 设备标识符类型
#[derive(Debug)]
#[derive(Clone)]
#[derive(Hash)]
#[derive(PartialOrd, PartialEq)]
#[derive(Ord, Eq)]
#[derive(Debug, Clone, Hash, PartialOrd, PartialEq, Ord, Eq)]
pub struct IdTable(&'static str, u32);

/// @brief: 设备标识符操作方法集
Expand All @@ -38,8 +34,7 @@ impl IdTable {
}

/// @brief: 设备当前状态
#[derive(Debug)]
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub enum DeviceState {
NotInitialized = 0,
Initialized = 1,
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/driver/base/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod device;
pub mod platform;
pub mod device;
Loading