From bdbfa532cd44e56e171db89ab6fbecf792609456 Mon Sep 17 00:00:00 2001 From: Alexander Boettcher Date: Wed, 26 Apr 2023 11:59:42 +0200 Subject: [PATCH] platform/x86: improve DMA memory alignment If the DMA memory allocation alignment is unfortunate (not natural size aligned), the IOMMU (AMD/INTEL) may not use larger(super) pages with effects on the page table walk frequency and TLB caching. Issue #4820 --- repos/os/src/drivers/platform/device_pd.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/repos/os/src/drivers/platform/device_pd.cc b/repos/os/src/drivers/platform/device_pd.cc index bffec91759..1dff174c43 100644 --- a/repos/os/src/drivers/platform/device_pd.cc +++ b/repos/os/src/drivers/platform/device_pd.cc @@ -94,7 +94,14 @@ addr_t Device_pd::_dma_addr(addr_t const phys_addr, }); } - return _dma_alloc.alloc_aligned(size, 12).convert( + /* natural size align (to some limit) for better IOMMU TLB usage */ + unsigned size_align_log2 = unsigned(log2(size)); + if (size_align_log2 < 12) /* 4 kB */ + size_align_log2 = 12; + if (size_align_log2 > 24) /* 16 MB */ + size_align_log2 = 24; + + return _dma_alloc.alloc_aligned(size, size_align_log2).convert( [&] (void *ptr) { return (addr_t)ptr; }, [&] (Alloc_error err) -> addr_t { switch (err) { @@ -102,6 +109,7 @@ addr_t Device_pd::_dma_addr(addr_t const phys_addr, case Alloc_error::OUT_OF_CAPS: throw Out_of_caps(); case Alloc_error::DENIED: error("Could not allocate DMA area of size: ", size, + " alignment: ", size_align_log2, " total avail: ", _dma_alloc.avail(), " (error: ", err, ")"); break;