Skip to contentSkip to conversation

pci: add bar sizing

PATCH New Michael S. Tsirkin 1 participant 2 messages Aug 27, 2015 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 pci: add bar sizing

Will be used for pci-testdev.

---
 lib/x86/pci.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
lib/x86/pci.c +23 -0
index 231668a..5116dac 100644
@@ -19,6 +19,13 @@ static uint32_t pci_config_read(pcidevaddr_t dev, uint8_t reg)
     return inl(0xCFC);
 }
 
+static uint32_t pci_config_write(pcidevaddr_t dev, uint8_t reg, uint32_t val)
+{
+    uint32_t index = reg | (dev << 8) | (0x1 << 31);
+    outl(0xCF8, index);
+    outl(0xCFC, val);
+}
+
 /* Scan bus look for a specific device. Only bus 0 scanned for now. */
 pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id)
 {
@@ -42,6 +49,22 @@ unsigned long pci_bar_addr(pcidevaddr_t dev, int bar_num)
     }
 }
 
+unsigned long pci_bar_size(pcidevaddr_t dev, int bar_num)
+{
+    uint32_t bar = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
+    uint32_t mask;
+
+    pci_config_write(dev, PCI_BASE_ADDRESS_0 + bar_num * 4, 0xffffffff);
+    mask = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
+    pci_config_write(dev, PCI_BASE_ADDRESS_0 + bar_num * 4, bar);
+
+    if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
+        return ~(mask & PCI_BASE_ADDRESS_IO_MASK) + 1;
+    } else {
+        return ~(mask & PCI_BASE_ADDRESS_MEM_MASK) + 1;
+    }
+}
+
 bool pci_bar_is_memory(pcidevaddr_t dev, int bar_num)
 {
     uint32_t bar = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
Signature · 1 line
MST
#1 lore raw

PATCH 2/2 pci-test: allow bar > 4k

vmexit using test assumes BAR size is exactly 4K.
Remove this restriction, allow any BAR size.

---
 x86/vmexit.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
x86/vmexit.c +11 -5
index 7e9af15..3ff7fbf 100644
@@ -407,7 +407,7 @@ bool test_wanted(struct test *test, char *wanted[], int nwanted)
 int main(int ac, char **av)
 {
 	int i;
-	unsigned long membar = 0, base, offset;
+	unsigned long membar = 0, base, offset, size;
 	void *m;
 	pcidevaddr_t pcidev;
 
@@ -425,12 +425,18 @@ int main(int ac, char **av)
 				continue;
 			}
 			if (pci_bar_is_memory(pcidev, i)) {
+				int p;
+
+				size = pci_bar_size(pcidev, i);
 				membar = pci_bar_addr(pcidev, i);
-				base = membar & ~4095;
+				base = membar & ~(size - 1);
 				offset = membar - base;
-				m = alloc_vpages(1);
-				
-				install_page((void *)read_cr3(), base, m);
+				m = alloc_vpages(size / 4096);
+
+				for (p = 0; p < size; p += 4096) {
+				    install_page((void *)read_cr3(), base + p,
+                                                 m + p);
+				}
 				pci_test.memaddr = m + offset;
 			} else {
 				pci_test.iobar = pci_bar_addr(pcidev, i);
Signature · 1 line
MST