Skip to content
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

dma: Remove use of Cell<Option<>> in DMA driver #1946

Merged
merged 3 commits into from
Feb 8, 2025
Merged

Conversation

swenson
Copy link
Contributor

@swenson swenson commented Feb 5, 2025

Generally we only like to keep one copy of AxiDmaReg in existence, and use Rust's borrowing rules around &mut to ensure that it is safely accessed. However, this is not generally possible when implementing the Mmio trait, since the method receiver must be immutable.

Cell<Option<>> or RefCell can provide interior mutability so that we can safely share mutable references from immutable receivers, but it complicates the code and adds bloat to the binary.

However, since Caliptra is single-threaded, as long as we are careful about how we structure the code so that we don't ever have two AxiDmaReg instances in existence at the same time (which might cause conflicts in programming the underlying registers), then we will have fulfilled the contract with the unsafe code. This safety is ensured as long as there are no nested callers of Dma::with_dma.

Based on feedback from #1904.

@swenson swenson added the Caliptra v2.0 Items to be considered for v2.0 Release label Feb 5, 2025
Generally we only like to keep one copy of `AxiDmaReg` in existence,
and use Rust's borrowing rules around `&mut` to ensure that it is safely
accessed. However, this is not generally possible when implementing the
`Mmio` trait, since it requires interior mutability.

`Cell<Option<>>` or `RefCell` can provide interior mutability so that we
can safely share mutable references from immutable receivers, but it
complicates the code and adds bloat to the binary.

However, since Caliptra is single-threaded, as long as we are careful
about how we structure the code so that we don't ever have two
`AxiDmaReg` instances in existence at the same time (which might cause
conflicts in programming the underlying registers), then we will have
fulfilled the contract with the `unsafe` code. This safety is ensured as
long as there are no nested callers of `Dma::with_dma`.
@mhatrevi mhatrevi enabled auto-merge (squash) February 7, 2025 23:16
@mhatrevi mhatrevi merged commit 0e60c3a into main-2.x Feb 8, 2025
9 checks passed
@swenson swenson deleted the clean-up-dma-cell branch February 8, 2025 21:21
@swenson
Copy link
Contributor Author

swenson commented Feb 8, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Caliptra v2.0 Items to be considered for v2.0 Release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants