diff mbox

[kvm-unit-tests,2/2] pci: Align PCI addresses on BAR size boundary

Message ID 89cae15d6c00605b61757624c8b37cae41e21497.1480069555.git.agordeev@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Alexander Gordeev Nov. 25, 2016, 10:29 a.m. UTC
PCI I/O and Memory must be allocated to a device in a naturally
aligned way. For example, if a device asks for 0xB0 of PCI I/O
space then it must be aligned on an address that is a multiple
of 0xB0 (http://www.tldp.org/LDP/tlk/dd/pci.html)

Cc: Thomas Huth <thuth@redhat.com>
Cc: Andrew Jones <drjones@redhat.com>
Reported-by: Andrew Jones <drjones@redhat.com>
Signed-off-by: Alexander Gordeev <agordeev@redhat.com>
---
 lib/pci-host-generic.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
diff mbox

Patch

diff --git a/lib/pci-host-generic.c b/lib/pci-host-generic.c
index df66bd4741e6..d57b47ee4c53 100644
--- a/lib/pci-host-generic.c
+++ b/lib/pci-host-generic.c
@@ -170,7 +170,7 @@  static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
 	struct pci_host_bridge *host = pci_host_bridge;
 	struct pci_addr_space *as = &host->addr_space[0];
 	u32 bar;
-	u64 size;
+	u64 size, pci_addr;
 	int type, i;
 
 	*addr = ~0;
@@ -199,8 +199,10 @@  static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
 		return false;
 	}
 
+	pci_addr = ALIGN(as->pci_start + as->allocated, size);
+	size += pci_addr - (as->pci_start + as->allocated);
 	assert(as->allocated + size <= as->size);
-	*addr = as->pci_start + as->allocated;
+	*addr = pci_addr;
 	as->allocated += size;
 
 	return true;