Adjust visibility marks in `ostd::io`

This commit is contained in:
Ruihan Li 2025-10-08 17:10:14 +08:00 committed by Tate, Hongliang Tian
parent 9062f86194
commit fa59b8be2b
4 changed files with 22 additions and 26 deletions

View File

@ -20,7 +20,7 @@ use crate::{
};
/// I/O memory allocator that allocates memory I/O access to device drivers.
pub struct IoMemAllocator {
pub(super) struct IoMemAllocator {
allocators: Vec<RangeAllocator>,
}
@ -28,11 +28,8 @@ impl IoMemAllocator {
/// Acquires the I/O memory access for `range`.
///
/// If the range is not available, then the return value will be `None`.
pub fn acquire(&self, range: Range<usize>) -> Option<IoMem> {
debug!(
"Try to acquire MMIO range:{:x?}..{:x?}",
range.start, range.end
);
pub(super) fn acquire(&self, range: Range<usize>) -> Option<IoMem> {
debug!("Try to acquire MMIO range: {:#x?}", range);
find_allocator(&self.allocators, &range)?
.alloc_specific(&range)
@ -48,10 +45,10 @@ impl IoMemAllocator {
///
/// The caller must have ownership of the MMIO region through the `IoMemAllocator::get` interface.
#[expect(dead_code)]
pub(in crate::io) unsafe fn recycle(&self, range: Range<usize>) {
let allocator = find_allocator(&self.allocators, &range).unwrap();
pub(super) unsafe fn recycle(&self, range: Range<usize>) {
debug!("Recycling MMIO range: {:#x?}", range);
debug!("Recycling MMIO range:{:x}..{:x}", range.start, range.end);
let allocator = find_allocator(&self.allocators, &range).unwrap();
allocator.free(range);
}
@ -113,7 +110,7 @@ impl IoMemAllocatorBuilder {
}
/// The I/O Memory allocator of the system.
pub static IO_MEM_ALLOCATOR: Once<IoMemAllocator> = Once::new();
pub(super) static IO_MEM_ALLOCATOR: Once<IoMemAllocator> = Once::new();
/// Initializes the static `IO_MEM_ALLOCATOR` based on builder.
///
@ -121,7 +118,7 @@ pub static IO_MEM_ALLOCATOR: Once<IoMemAllocator> = Once::new();
///
/// User must ensure all the memory I/O regions that belong to the system device have been removed by calling the
/// `remove` function.
pub(crate) unsafe fn init(io_mem_builder: IoMemAllocatorBuilder) {
pub(in crate::io) unsafe fn init(io_mem_builder: IoMemAllocatorBuilder) {
// SAFETY: The safety is upheld by the caller.
IO_MEM_ALLOCATOR.call_once(|| unsafe { IoMemAllocator::new(io_mem_builder.allocators) });
}

View File

@ -14,7 +14,7 @@ use crate::{
};
/// I/O port allocator that allocates port I/O access to device drivers.
pub struct IoPortAllocator {
pub(super) struct IoPortAllocator {
/// Each ID indicates whether a Port I/O (1B) is allocated.
///
/// Instead of using `RangeAllocator` like `IoMemAllocator` does, it is more reasonable to use `IdAlloc`,
@ -24,9 +24,11 @@ pub struct IoPortAllocator {
impl IoPortAllocator {
/// Acquires the `IoPort`. Return None if any region in `port` cannot be allocated.
pub fn acquire<T, A>(&self, port: u16) -> Option<IoPort<T, A>> {
let mut allocator = self.allocator.lock();
pub(super) fn acquire<T, A>(&self, port: u16) -> Option<IoPort<T, A>> {
let mut range = port..(port + size_of::<T>() as u16);
debug!("Try to acquire PIO range: {:#x?}", range);
let mut allocator = self.allocator.lock();
if range.any(|i| allocator.is_allocated(i as usize)) {
return None;
}
@ -44,8 +46,8 @@ impl IoPortAllocator {
/// # Safety
///
/// The caller must have ownership of the PIO region through the `IoPortAllocator::acquire` interface.
pub(in crate::io) unsafe fn recycle(&self, range: Range<u16>) {
debug!("Recycling MMIO range: {:#x?}", range);
pub(super) unsafe fn recycle(&self, range: Range<u16>) {
debug!("Recycling PIO range: {:#x?}", range);
self.allocator
.lock()
@ -66,7 +68,7 @@ pub(super) static IO_PORT_ALLOCATOR: Once<IoPortAllocator> = Once::new();
///
/// 2. `MAX_IO_PORT` defined in `crate::arch::io` is guaranteed not to exceed the maximum
/// value specified by architecture.
pub(crate) unsafe fn init() {
pub(in crate::io) unsafe fn init() {
// SAFETY: `MAX_IO_PORT` is guaranteed not to exceed the maximum value specified by architecture.
let mut allocator = IdAlloc::with_capacity(crate::arch::io::MAX_IO_PORT as usize);

View File

@ -48,12 +48,12 @@ impl<T, A> IoPort<T, A> {
size_of::<T>() as u16
}
/// Create an I/O port.
/// Creates an I/O port.
///
/// # Safety
///
/// This function is marked unsafe as creating an I/O port is considered
/// a privileged operation.
/// Reading from or writing to the I/O port may have side effects. Those side effects must not
/// cause soundness problems (e.g., they must not corrupt the kernel memory).
pub const unsafe fn new(port: u16) -> Self {
Self {
port,
@ -65,7 +65,6 @@ impl<T, A> IoPort<T, A> {
impl<T: PortRead, A: IoPortReadAccess> IoPort<T, A> {
/// Reads from the I/O port
#[inline]
pub fn read(&self) -> T {
unsafe { PortRead::read_from_port(self.port) }
}
@ -73,7 +72,6 @@ impl<T: PortRead, A: IoPortReadAccess> IoPort<T, A> {
impl<T: PortWrite, A: IoPortWriteAccess> IoPort<T, A> {
/// Writes to the I/O port
#[inline]
pub fn write(&self, value: T) {
unsafe { PortWrite::write_to_port(self.port, value) }
}

View File

@ -9,15 +9,14 @@
mod io_mem;
use cfg_if::cfg_if;
pub use self::io_mem::IoMem;
pub(crate) use self::io_mem::IoMemAllocatorBuilder;
cfg_if!(
cfg_if::cfg_if!(
if #[cfg(target_arch = "x86_64")] {
mod io_port;
pub use io_port::IoPort;
pub use self::io_port::IoPort;
pub(crate) use self::io_port::{reserve_io_port_range, sensitive_io_port, RawIoPortRange};
}
);