Skip to content

Feat: IoMmu protocol#1728

Open
PelleKrab wants to merge 7 commits intorust-osdev:mainfrom
PelleKrab:Iommu
Open

Feat: IoMmu protocol#1728
PelleKrab wants to merge 7 commits intorust-osdev:mainfrom
PelleKrab:Iommu

Conversation

@PelleKrab
Copy link
Copy Markdown
Contributor

This PR adds the IoMmu Protocol to uefi-rs, enabling easy DMA (Direct Memory Access) for devices. This is a draft to get some initial feedback before writing uefi-rs tests. It is already working in my internal project. The main areas I would like feedback on are:

  • Mapping and DMA Buffer Types: Is this too much abstraction? Should we simplify it by requiring the developer to manually call unmap() and free_buffer()?
  • Should free_buffer_raw() and unmap_raw() be made public?
  • Repository Organization: Is the general organization within the repo appropriate?

General Implementation:

// Allocate DMA buffer
let sz = some_buffer.len();
let page_num = bytes_to_pages(sz);
let mut host_buffer = iommu.allocate_buffer(
    MemoryType::BOOT_SERVICES_DATA,
    page_num,
    EdkiiIommuAttribute::MEMORY_CACHED,
)?;

// Map buffer for DMA
let (device_addr, mapping, wrt_size) = iommu.map(
    EdkiiIommuOperation::BUS_MASTER_COMMON_BUFFER,
    &host_buffer,
    size,
)?;

// Give permissions to the specified device 
iommu.set_attribute(some_device_handle, &mapping, EdkiiIommuAccess::READ)?;

// Copy firmware to DMA buffer
let dma_buf = host_buffer.deref_mut();
if sz > wrt_size {
    return Status::BUFFER_TOO_SMALL;
}
dma_buf[..sz].copy_from_slice(&some_buffer[..sz]);

// Notify the Device and pass the `device_addr`

ref:
#1723

@nicholasbishop
Copy link
Copy Markdown
Member

Thanks for working on this :) I haven't had a chance to look at the uefi wrapper in detail yet, but overall it looks reasonable. Could you split the uefi-raw part out to a separate PR? That should be straightforward to review and merge in isolation.

@nicholasbishop
Copy link
Copy Markdown
Member

Is this ready for review? Currently it's marked as draft still. (No worries if that's intentional, just want to be sure you're not waiting on review.)

@PelleKrab
Copy link
Copy Markdown
Contributor Author

I still need to write some test for it. I have been busy with some other things, so I'll send those over as soon as I can. Feel free to take a look if you want.

@PelleKrab
Copy link
Copy Markdown
Contributor Author

@nicholasbishop I am trying to write some test for this, but I cant seem to get a protocol handle. I went into the uefi shell of the qemu vm, and the IoMmu driver image is present:

dh -p 4e939de9-d948-4b0f-88ed-e6e1ce517c1e -v
Handle dump by protocol '4E939DE9-D948-4B0F-88ED-E6E1CE517C1E'
Shell> dh -p 03c4e603-ac28-11d3-9a2d-0090273fc14d -v
Handle dump by protocol 'PXEBaseCode'
Shell> dh 28 -v
28: 3ECF0C18
ImageDevicePath(3ECF2A98)
  Fv(7CB8BDC9-F8EB-4F34-AAEA-3EE4AF6516A1)/FvFile(8657015B-EA43-440D-949A-AF3BE365C0FC)
LoadedImage(3ECF20C0)
  Name..........: IoMmuDxe

So I am assuming the driver is failing to find hardware. I have tried a few different things, different EDK2 builds and followed the virtio instructions here, but nothing has worked. Ik it worked on hardware, so if you have any ideas on how to get qemu setup for IoMmu please let me know.

@phip1611
Copy link
Copy Markdown
Member

So I am assuming the driver is failing to find hardware. I have tried a few different things, different EDK2 builds and followed the virtio instructions here, but nothing has worked.

Thanks for trying to find a solution! Is there any update here? Have you tried to add an IOMMU to the QEMU VM? Perhaps one is needed so that the protocol will be installed one some handles (-device intel-iommu)?

@phip1611
Copy link
Copy Markdown
Member

Any update here @PelleKrab ? If not, I'm in favor of closing the PR for now.

@phip1611 phip1611 changed the title Feat: IoMmu protocol [STALE] Feat: IoMmu protocol Jan 25, 2026
@PelleKrab
Copy link
Copy Markdown
Contributor Author

Any update here @PelleKrab ? If not, I'm in favor of closing the PR for now.

No significant progress yet. I've been working on this here and there. Right now, I'm building a custom OVMF image to see if that forces the xtask VM to recognize the IOMMU drivers. I'm not sure if there’s wider interest in that approach, but I'm giving it a try since no other QEMU environment configs seem to help. I'm fine with closing the issue for now, and I can reopen it if I find a solution.

@phip1611
Copy link
Copy Markdown
Member

phip1611 commented Jan 26, 2026

I'm fine with closing the issue for now, and I can reopen it if I find a solution.

Thanks! Good luck with your investigations. We are very happy for every contribution and feel free to reopen / open a new one as soon as you have a solution.

For good project hygiene, we aim for short-living PRs (less than 3 months best)

@phip1611 phip1611 closed this Jan 26, 2026
@PelleKrab
Copy link
Copy Markdown
Contributor Author

@phip1611 I got the tests to run! It will require some changes to omvf-prebuilt, check out my branch here: link. If these additions are fine, let me know. Then I can open a PR there and we can reopen this PR with the new test cases.

@phip1611
Copy link
Copy Markdown
Member

very cool! regarding ovmf-prebuilt I think @nicholasbishop is the expert - from my side it is looking good

@PelleKrab
Copy link
Copy Markdown
Contributor Author

@phip1611 Everything is working now! Could you reopen this and take a look?

@phip1611 phip1611 reopened this Mar 11, 2026
@phip1611
Copy link
Copy Markdown
Member

phip1611 commented Mar 11, 2026

Please rebase / solve the conflict and remove all Merge branch 'main' into Iommu commits! We don't do that, we use the rebase flow for feature branches :)

@phip1611 phip1611 marked this pull request as ready for review March 11, 2026 11:41
@phip1611 phip1611 changed the title [STALE] Feat: IoMmu protocol Feat: IoMmu protocol Mar 11, 2026
@PelleKrab
Copy link
Copy Markdown
Contributor Author

The x86_64 CI jobs now require QEMU 9+ because they use -device intel-iommu. We can pin the CI to Ubuntu 25.10 (QEMU 10.1) for now or wait for the next LTS release. Additionally, both the new OVMF and the versions prior to the IOMMU changes seem to fail the HTTPS test. Let me know if you have any suggestions on that front.

@phip1611
Copy link
Copy Markdown
Member

thanks for working on that! @nicholasbishop any idea why this fails?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants