Skip to content

Commit 0582009

Browse files
hoshinolinajannau
authored andcommitted
rust: Add ioremap_np support to io_mem & friends
Apple SoCs require non-posted mappings for MMIO, and this is automatically handled by devm_ioremap_resource() and friends via a resource flag. Implement the same logic in kernel::io_mem, so it can work the same way. Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent f96a3c5 commit 0582009

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

rust/helpers/iomem.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ void __iomem *rust_helper_ioremap(resource_size_t offset, unsigned long size)
77
return ioremap(offset, size);
88
}
99

10+
void __iomem *rust_helper_ioremap_np(resource_size_t offset, unsigned long size)
11+
{
12+
return ioremap_np(offset, size);
13+
}
14+
1015
void rust_helper_memcpy_fromio(void *to, const volatile void __iomem *from, long count)
1116
{
1217
memcpy_fromio(to, from, count);

rust/kernel/io_mem.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ pub enum IoResource {
1919
pub struct Resource {
2020
offset: bindings::resource_size_t,
2121
size: bindings::resource_size_t,
22+
flags: core::ffi::c_ulong,
2223
}
2324

2425
impl Resource {
2526
pub(crate) fn new(
2627
start: bindings::resource_size_t,
2728
end: bindings::resource_size_t,
29+
flags: core::ffi::c_ulong,
2830
) -> Option<Self> {
2931
if start == 0 {
3032
return None;
3133
}
3234
Some(Self {
3335
offset: start,
3436
size: end.checked_sub(start)?.checked_add(1)?,
37+
flags,
3538
})
3639
}
3740
}
@@ -160,8 +163,14 @@ impl<const SIZE: usize> IoMem<SIZE> {
160163
}
161164

162165
// Try to map the resource.
163-
// SAFETY: Just mapping the memory range.
164-
let addr = unsafe { bindings::ioremap(res.offset, res.size as _) };
166+
let addr = if res.flags & (bindings::IORESOURCE_MEM_NONPOSTED as core::ffi::c_ulong) != 0 {
167+
// SAFETY: Just mapping the memory range.
168+
unsafe { bindings::ioremap_np(res.offset, res.size as _) }
169+
} else {
170+
// SAFETY: Just mapping the memory range.
171+
unsafe { bindings::ioremap(res.offset, res.size as _) }
172+
};
173+
165174
if addr.is_null() {
166175
Err(ENOMEM)
167176
} else {

rust/kernel/platform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl Device {
247247

248248
// SAFETY: The pointer `res` is returned from `bindings::platform_get_resource`
249249
// above and checked if it is not a NULL.
250-
unsafe { Resource::new((*res).start, (*res).end) }.ok_or(EINVAL)
250+
unsafe { Resource::new((*res).start, (*res).end, (*res).flags) }.ok_or(EINVAL)
251251
}
252252

253253
/// Ioremaps resources of a platform device.

0 commit comments

Comments
 (0)