forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 465
rust: platform: add ioremap_resource
and get_resource
methods
#682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
m-falkowski1
wants to merge
1
commit into
Rust-for-Linux:rust
Choose a base branch
from
m-falkowski1:add_platform_ioremap_resource
base: rust
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,16 @@ | |
//! C header: [`include/linux/platform_device.h`](../../../../include/linux/platform_device.h) | ||
|
||
use crate::{ | ||
bindings, c_types, | ||
bindings, bit, c_types, | ||
device::{self, RawDevice}, | ||
driver, | ||
error::{from_kernel_result, Result}, | ||
error::{code::*, from_kernel_result}, | ||
io_mem::{IoMem, IoResource, Resource}, | ||
of, | ||
str::CStr, | ||
to_result, | ||
types::PointerWrapper, | ||
ThisModule, | ||
Result, ThisModule, | ||
}; | ||
|
||
/// A registration of a platform driver. | ||
|
@@ -161,6 +162,7 @@ pub trait Driver { | |
/// The field `ptr` is non-null and valid for the lifetime of the object. | ||
pub struct Device { | ||
ptr: *mut bindings::platform_device, | ||
used_resource: u64, | ||
} | ||
|
||
impl Device { | ||
|
@@ -172,14 +174,68 @@ impl Device { | |
/// instance. | ||
unsafe fn from_ptr(ptr: *mut bindings::platform_device) -> Self { | ||
// INVARIANT: The safety requirements of the function ensure the lifetime invariant. | ||
Self { ptr } | ||
Self { | ||
ptr, | ||
used_resource: 0, | ||
} | ||
} | ||
|
||
/// Returns id of the platform device. | ||
pub fn id(&self) -> i32 { | ||
// SAFETY: By the type invariants, we know that `self.ptr` is non-null and valid. | ||
unsafe { (*self.ptr).id } | ||
} | ||
|
||
/// Gets a system resources of a platform device. | ||
pub fn get_resource(&mut self, rtype: IoResource, num: usize) -> Result<Resource> { | ||
// SAFETY: `self.ptr` is valid by the type invariant. | ||
let res = unsafe { bindings::platform_get_resource(self.ptr, rtype as _, num as _) }; | ||
if res.is_null() { | ||
return Err(EINVAL); | ||
} | ||
|
||
// Get the position of the found resource in the array. | ||
// SAFETY: | ||
// - `self.ptr` is valid by the type invariant. | ||
// - `res` is a displaced pointer to one of the array's elements, | ||
// and `resource` is its base pointer. | ||
let index = unsafe { res.offset_from((*self.ptr).resource) } as usize; | ||
|
||
// Make sure that the index does not exceed the 64-bit mask. | ||
assert!(index < 64); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't necessarily true, is it? Instead of an |
||
|
||
if self.used_resource >> index & bit(0) == 1 { | ||
return Err(EBUSY); | ||
} | ||
self.used_resource |= bit(index); | ||
|
||
// SAFETY: The pointer `res` is returned from `bindings::platform_get_resource` | ||
// above and checked if it is not a NULL. | ||
unsafe { Resource::new((*res).start, (*res).end) }.ok_or(EINVAL) | ||
} | ||
|
||
/// Ioremaps resources of a platform device. | ||
/// | ||
/// # Safety | ||
/// | ||
/// Callers must ensure that either (a) the resulting interface cannot be used to initiate DMA | ||
/// operations, or (b) that DMA operations initiated via the returned interface use DMA handles | ||
/// allocated through the `dma` module. | ||
pub unsafe fn ioremap_resource<const SIZE: usize>( | ||
&mut self, | ||
index: usize, | ||
) -> Result<IoMem<SIZE>> { | ||
let mask = self.used_resource; | ||
let res = self.get_resource(IoResource::Mem, index)?; | ||
|
||
// SAFETY: Valid by the safety contract. | ||
let iomem = unsafe { IoMem::<SIZE>::try_new(res) }; | ||
// If remapping fails, the given resource won't be used, so restore the old mask. | ||
if iomem.is_err() { | ||
self.used_resource = mask; | ||
} | ||
iomem | ||
} | ||
} | ||
|
||
// SAFETY: The device returned by `raw_device` is the raw platform device. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This happens to be how this is implemented today, but isn't necessarily going to always be this way. So I suggest we explicitly test here that the pointer is between
self.ptr.resource
andself.ptr.resource + 64
. If it isn't, we should fail the request.