1#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
2use core::ops::{BitAnd, BitOr, Not};
3use core::{mem::MaybeUninit, ptr};
4
5use super::io::Io;
6
7#[repr(transparent)]
8pub struct Mmio<T> {
9 value: MaybeUninit<T>,
10}
11
12impl<T> Mmio<T> {
13 pub unsafe fn zeroed() -> Self {
14 Self {
15 value: MaybeUninit::zeroed(),
16 }
17 }
18 pub unsafe fn uninit() -> Self {
19 Self {
20 value: MaybeUninit::uninit(),
21 }
22 }
23 pub const fn from(value: T) -> Self {
24 Self {
25 value: MaybeUninit::new(value),
26 }
27 }
28}
29
30#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
32impl<T> Io for Mmio<T>
33where
34 T: Copy + PartialEq + BitAnd<Output = T> + BitOr<Output = T> + Not<Output = T>,
35{
36 type Value = T;
37
38 fn read(&self) -> T {
39 unsafe { ptr::read_volatile(ptr::addr_of!(self.value).cast::<T>()) }
40 }
41
42 fn write(&mut self, value: T) {
43 unsafe { ptr::write_volatile(ptr::addr_of_mut!(self.value).cast::<T>(), value) };
44 }
45}
46
47#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
49impl Io for Mmio<u8> {
50 type Value = u8;
51
52 fn read(&self) -> Self::Value {
53 unsafe {
54 let value: Self::Value;
55 let ptr: *const Self::Value = ptr::addr_of!(self.value).cast::<Self::Value>();
56 core::arch::asm!(
57 "mov {}, [{}]",
58 out(reg_byte) value,
59 in(reg) ptr
60 );
61 value
62 }
63 }
64
65 fn write(&mut self, value: Self::Value) {
66 unsafe {
67 let ptr: *mut Self::Value = ptr::addr_of_mut!(self.value).cast::<Self::Value>();
68 core::arch::asm!(
69 "mov [{}], {}",
70 in(reg) ptr,
71 in(reg_byte) value,
72 );
73 }
74 }
75}
76
77#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
79impl Io for Mmio<u16> {
80 type Value = u16;
81
82 fn read(&self) -> Self::Value {
83 unsafe {
84 let value: Self::Value;
85 let ptr: *const Self::Value = ptr::addr_of!(self.value).cast::<Self::Value>();
86 core::arch::asm!(
87 "mov {:x}, [{}]",
88 out(reg) value,
89 in(reg) ptr
90 );
91 value
92 }
93 }
94
95 fn write(&mut self, value: Self::Value) {
96 unsafe {
97 let ptr: *mut Self::Value = ptr::addr_of_mut!(self.value).cast::<Self::Value>();
98 core::arch::asm!(
99 "mov [{}], {:x}",
100 in(reg) ptr,
101 in(reg) value,
102 );
103 }
104 }
105}
106
107#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
109impl Io for Mmio<u32> {
110 type Value = u32;
111
112 fn read(&self) -> Self::Value {
113 unsafe {
114 let value: Self::Value;
115 let ptr: *const Self::Value = ptr::addr_of!(self.value).cast::<Self::Value>();
116 core::arch::asm!(
117 "mov {:e}, [{}]",
118 out(reg) value,
119 in(reg) ptr
120 );
121 value
122 }
123 }
124
125 fn write(&mut self, value: Self::Value) {
126 unsafe {
127 let ptr: *mut Self::Value = ptr::addr_of_mut!(self.value).cast::<Self::Value>();
128 core::arch::asm!(
129 "mov [{}], {:e}",
130 in(reg) ptr,
131 in(reg) value,
132 );
133 }
134 }
135}
136
137#[cfg(target_arch = "x86_64")]
139impl Io for Mmio<u64> {
140 type Value = u64;
141
142 fn read(&self) -> Self::Value {
143 unsafe {
144 let value: Self::Value;
145 let ptr: *const Self::Value = ptr::addr_of!(self.value).cast::<Self::Value>();
146 core::arch::asm!(
147 "mov {:r}, [{}]",
148 out(reg) value,
149 in(reg) ptr
150 );
151 value
152 }
153 }
154
155 fn write(&mut self, value: Self::Value) {
156 unsafe {
157 let ptr: *mut Self::Value = ptr::addr_of_mut!(self.value).cast::<Self::Value>();
158 core::arch::asm!(
159 "mov [{}], {:r}",
160 in(reg) ptr,
161 in(reg) value,
162 );
163 }
164 }
165}