Skip to content

Commit 1d00b9d

Browse files
WhatAmISupposedToPutHerejannau
authored andcommitted
rust: Add helper for memcpy_toio
Signed-off-by: Sasha Finkelstein <fnkl.kernel@gmail.com>
1 parent ca880c8 commit 1d00b9d

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

rust/helpers/iomem.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ void rust_helper_memcpy_fromio(void *to, const volatile void __iomem *from, long
1616
{
1717
memcpy_fromio(to, from, count);
1818
}
19+
20+
void rust_helper_memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
21+
{
22+
memcpy_toio(to, from, count);
23+
}

rust/kernel/io_mem.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,27 @@ impl<const SIZE: usize> IoMem<SIZE> {
243243
Ok(())
244244
}
245245

246+
/// Copy memory block to i/o memory from the specified buffer.
247+
pub fn try_memcpy_toio(&self, offset: usize, buffer: &[u8]) -> Result {
248+
if !Self::offset_ok_of_val(offset, buffer) {
249+
return Err(EINVAL);
250+
}
251+
252+
let ptr = self.ptr.wrapping_add(offset);
253+
254+
// SAFETY:
255+
// - The type invariants guarantee that `ptr` is a valid pointer.
256+
// - The bounds of `buffer` are checked with a call to `offset_ok_of_val()`.
257+
unsafe {
258+
bindings::memcpy_toio(
259+
ptr as *mut _,
260+
buffer.as_ptr() as *const _,
261+
buffer.len() as _,
262+
)
263+
};
264+
Ok(())
265+
}
266+
246267
define_read!(readb, try_readb, u8);
247268
define_read!(readw, try_readw, u16);
248269
define_read!(readl, try_readl, u32);

0 commit comments

Comments
 (0)