Skip to contentSkip to conversation

VT-d unit test

PATCH Superseded Peter Xu 3 participants 28 messages Nov 23, 2016 – Nov 29, 2016

Subsystems (2)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

v6 cover VT-d unit test

This is v6 of vt-d unit test series.

Patch "libcflat: add IS_ALIGNED() macro, and page sizes" is picked up by Drew in the ARM GIC framework series, so please feel free to drop it when needed.

Online repo:

https://github.com/xzpeter/kvm-unit-tests.git iommu-ut-v6

Please review. Thanks,

v6:
- pci: rename pci_dev.bar to pcidev.resource (to Drew: I didn't remove your r-b in several patches due to this change, assuming the change is trivial) [Alex]
- pci_cap_walk(): add an assert to avoid dead loop [Alex]
- pci_cap_walk(): another assertion to make sure cap_id is valid [Drew]

v5:
- add one more patch at the beginning of series to add "extern" to pci_testdev() header definition [Drew]
- add "extern" to all pci.h functions [Drew]
- update IS_ALIGNED patch to be aligned with Drew's gic series [Drew]
- replace one more PCI_DEVFN_MAX/PCI_BAR_NUM in lib/pci-host-generic.c [Drew]
- add {} in vtd_install_pte() when there are one line code and one line comment [Drew]
- drop Drew's suggested-by in patch "x86: intel-iommu: add IR MSI test" (I forgot why I added it, but anyway let me drop it here...) [Drew]

v4:
(same as v3, but with correct patches sent out)

v3:
- is_power_of_2(): remove outer "()" [Drew]
- fix indent for vtd_root_table() [Drew]
- pci_find_dev(): keep {} to avoid touching more codes [Drew]
- pci_scan_bars(): check whether bar is 64bits [Drew]
- pci_enable_defaults(): make it return nothing [Drew]
- fix strnang blanks in lib/x86/intel-iommu.h [Drew]
- pci_setup_msi(): fix correct assert() inside [Drew]
- squash patches: 03+17 08+09 10+11 12+14 [Drew]
- remove all "()" for raw defines (numbers) in macros [Drew]

v2:
- move cpu_relax patch to the beginning, and use them in all places [Drew]
- replace all corresponding 256 into PCI_DEVFN_MAX, as well for PCI_BAR_NUM [Drew]
- adding is_power_of_2() to replace ONE_BIT_ONLY() [Drew]
- add SZ_64K macro [Drew]
- declare pci_config_write[wb] in lib/asm-generic/pci-host-bridge.h [Alex]
- edu_reg_read/write() add "l" in func name [Drew]
- drop pci_set_master(), instead, provide pci_cmd_set_clr() [Drew]
- change return code into bool (always) for functions that apply [Drew]
- keep old pci_find_dev() interface [Drew/Alex]
- use __raw_{read|write}*() for both vt-d and edu register read/writes [Alex]
- remove pci_ prefix for all pci_dev fields [Drew]
- replace 0xff in cap_handlers[0xff] into (PCI_CAP_ID_MAX + 1) [Drew]
- make x86/unittest.cfg simpler by using q35 directly with eim=off [Drew]

RFC -> v1:
- when init edu device fail, report_skip() rather than return error [Radim]
- use asserts rather than "static bool inited" to avoid multiple init of components (affects patch 1/2) [Drew]
- moving the first two patches out of the series [Drew]
- int vtd_init(), do not setup_idt() since smp_init() did it [Drew]
- when edu do not have MSI enabled, skip interrupt test [Radim]
- rename vtd_reg_*() into vtd_{read|write}[lq](), and move them to header file [Drew]
- use PAGE_MASK when able [Drew]
- use "&" instead of "|" in intel-iommu init test (three places) [Drew]
- use "vtd_init()" in unit test [Drew]
- mention that where intel-iommu.h comes from [Drew]
- re-written vtd_gcmd_or(), make it also work on even hardware [Drew]
- remove most of the oneline wrapper for VT-d registers, instead, use vtd_{read|write}* with register names [Drew]
- remove useless BDF helpers [Drew]
- move edu device macros into header file [Drew]
- make edu_check_alive static inline [Drew]
- remove all useless wrappers in pci-edu.c [Drew]
- remove pci_dma_dir_t and all its users, instead, use "bool from_device" [Drew]
- not use typedef for structs, to follow Linux/kvm-unit-tests coding style [Drew]
- let pci_dev_init() clean and simple, then provide pci_enable_defaults() for more complicated things [Drew]
- add one more patch to add intel-iommu test into x86/unittest [Radim]
- use 0x60 intr request instead of factorial to trigger edu device interrupt [Drew]
- ...and some other changes I just forgot to note down...

Currently only a very small test scope is covered:

* VT-d init
* DMAR: 4 bytes copy
* IR: MSI

However this series could be a base point to add more test cases for VT-d. The problem is, there are many IOMMU error conditions which are very hard to be triggered in a real guest (IOMMU has merely no interface for guest user, and it's totally running in the background). This piece of work can be a start point if we want to do more complicated things and play around with Intel IOMMU devices (also for IOMMU regression tests).

Please review. Thanks,

Peter Xu (14):
pci: fix missing extern for pci_testdev() x86/asm: add cpu_relax()
libcflat: introduce is_power_of_2()
x86: intel-iommu: add vt-d init test
libcflat: add IS_ALIGNED() macro, and page sizes
libcflat: moving MIN/MAX here
vm/page: provide PGDIR_OFFSET() macro
pci: introduce struct pci_dev
pci: provide pci_scan_bars()
pci: provide pci_enable_defaults()
pci: edu: introduce pci-edu helpers
x86: intel-iommu: add dmar test
pci: add msi support for 32/64bit address
x86: intel-iommu: add IR MSI test

 lib/alloc.c            |   3 -
 lib/libcflat.h         |  14 +++
 lib/pci-edu.c          |  73 ++++++++++++
 lib/pci-edu.h          |  83 +++++++++++++
 lib/pci-host-generic.c |  13 +-
 lib/pci-testdev.c      |  10 +-
 lib/pci.c              | 161 +++++++++++++++++++++----
 lib/pci.h              |  42 +++++--
 lib/x86/asm/barrier.h  |  11 ++
 lib/x86/asm/page.h     |   3 +
 lib/x86/intel-iommu.c  | 314 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/intel-iommu.h  | 142 ++++++++++++++++++++++
 lib/x86/vm.c           |   4 +-
 x86/Makefile.common    |   1 +
 x86/Makefile.x86_64    |   2 +
 x86/intel-iommu.c      | 120 +++++++++++++++++++
 x86/unittests.cfg      |   7 ++
 x86/vmexit.c           |  27 ++---
 18 files changed, 969 insertions(+), 61 deletions(-)
 create mode 100644 lib/pci-edu.c
 create mode 100644 lib/pci-edu.h
 create mode 100644 lib/x86/intel-iommu.c
 create mode 100644 lib/x86/intel-iommu.h
 create mode 100644 x86/intel-iommu.c
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v6 1/14 pci: fix missing extern for pci_testdev()

---
 lib/pci.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/pci.h +1 -1
index 30f5381..86d6b42 100644
@@ -43,7 +43,7 @@ extern bool pci_bar_is_valid(pcidevaddr_t dev, int bar_num);
 extern void pci_bar_print(pcidevaddr_t dev, int bar_num);
 extern void pci_dev_print_id(pcidevaddr_t dev);
 
-int pci_testdev(void);
+extern int pci_testdev(void);
 
 /*
  * pci-testdev is a driver for the pci-testdev qemu pci device. The
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH v6 2/14 x86/asm: add cpu_relax()

In reply to Peter Xu

This will be useful to be put inside loops.

---
 lib/x86/asm/barrier.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/x86/asm/barrier.h +11 -0
index 7c108bd..193fb4c 100644
@@ -13,4 +13,15 @@
 #define smp_rmb()	barrier()
 #define smp_wmb()	barrier()
 
+/* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
+static inline void rep_nop(void)
+{
+	asm volatile("rep; nop" ::: "memory");
+}
+
+static inline void cpu_relax(void)
+{
+	rep_nop();
+}
+
 #endif
Signature · 1 line
2.7.4
author
#3 lore raw

PATCH v6 3/14 libcflat: introduce is_power_of_2()

In reply to Peter Xu
---
 lib/libcflat.h | 5 +++++
 1 file changed, 5 insertions(+)
lib/libcflat.h +5 -0
index 72b1bf9..038ea1d 100644
@@ -103,4 +103,9 @@ do {									\
 	}								\
 } while (0)
 
+static inline bool is_power_of_2(unsigned long n)
+{
+	return n && !(n & (n - 1));
+}
+
 #endif
Signature · 1 line
2.7.4
author
#4 lore raw

PATCH v6 4/14 x86: intel-iommu: add vt-d init test

In reply to Peter Xu

Adding fundamental init test for Intel IOMMU. This includes basic initialization of Intel IOMMU device, like DMAR (DMA Remapping), IR (Interrupt Remapping), QI (Queue Invalidation), etc.

Further tests can use vtd_init() to initialize Intel IOMMU environment.

x86/unittests is updated to add this test.

---
 lib/x86/intel-iommu.c |  88 +++++++++++++++++++++++++++++++++++++
 lib/x86/intel-iommu.h | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/Makefile.x86_64   |   2 +
 x86/intel-iommu.c     |  27 ++++++++++++
 x86/unittests.cfg     |   7 +++
 5 files changed, 242 insertions(+)
 create mode 100644 lib/x86/intel-iommu.c
 create mode 100644 lib/x86/intel-iommu.h
 create mode 100644 x86/intel-iommu.c
lib/x86/intel-iommu.c new file +88 -0
new file mode 100644
index 0000000..9890f34
@@ -0,0 +1,88 @@
+/*
+ * Intel IOMMU APIs
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * Authors:
+ *   Peter Xu <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or
+ * later.
+ */
+
+#include "intel-iommu.h"
+
+#define VTD_RTA_MASK  (PAGE_MASK)
+#define VTD_IRTA_MASK (PAGE_MASK)
+
+static uint64_t vtd_root_table(void)
+{
+	/* No extend root table support yet */
+	return vtd_readq(DMAR_RTADDR_REG) & VTD_RTA_MASK;
+}
+
+static uint64_t vtd_ir_table(void)
+{
+	return vtd_readq(DMAR_IRTA_REG) & VTD_IRTA_MASK;
+}
+
+static void vtd_gcmd_or(uint32_t cmd)
+{
+	uint32_t status;
+
+	/* We only allow set one bit for each time */
+	assert(is_power_of_2(cmd));
+
+	status = vtd_readl(DMAR_GSTS_REG);
+	vtd_writel(DMAR_GCMD_REG, status | cmd);
+
+	if (cmd & VTD_GCMD_ONE_SHOT_BITS) {
+		/* One-shot bits are taking effect immediately */
+		return;
+	}
+
+	/* Make sure IOMMU handled our command request */
+	while (!(vtd_readl(DMAR_GSTS_REG) & cmd))
+		cpu_relax();
+}
+
+static void vtd_dump_init_info(void)
+{
+	printf("VT-d version:   0x%x\n", vtd_readl(DMAR_VER_REG));
+	printf("     cap:       0x%016lx\n", vtd_readq(DMAR_CAP_REG));
+	printf("     ecap:      0x%016lx\n", vtd_readq(DMAR_ECAP_REG));
+}
+
+static void vtd_setup_root_table(void)
+{
+	void *root = alloc_page();
+
+	memset(root, 0, PAGE_SIZE);
+	vtd_writeq(DMAR_RTADDR_REG, virt_to_phys(root));
+	vtd_gcmd_or(VTD_GCMD_ROOT);
+	printf("DMAR table address: 0x%016lx\n", vtd_root_table());
+}
+
+static void vtd_setup_ir_table(void)
+{
+	void *root = alloc_page();
+
+	memset(root, 0, PAGE_SIZE);
+	/* 0xf stands for table size (2^(0xf+1) == 65536) */
+	vtd_writeq(DMAR_IRTA_REG, virt_to_phys(root) | 0xf);
+	vtd_gcmd_or(VTD_GCMD_IR_TABLE);
+	printf("IR table address: 0x%016lx\n", vtd_ir_table());
+}
+
+void vtd_init(void)
+{
+	setup_vm();
+	smp_init();
+
+	vtd_dump_init_info();
+	vtd_gcmd_or(VTD_GCMD_QI); /* Enable QI */
+	vtd_setup_root_table();
+	vtd_setup_ir_table();
+	vtd_gcmd_or(VTD_GCMD_DMAR); /* Enable DMAR */
+	vtd_gcmd_or(VTD_GCMD_IR);   /* Enable IR */
+}
lib/x86/intel-iommu.h new file +118 -0

118 lines not on this page · raw message

x86/Makefile.x86_64 +2 -0
index f82492b..3e2821e 100644
@@ -4,6 +4,7 @@ ldarch = elf64-x86-64
 CFLAGS += -mno-red-zone
 
 cflatobjs += lib/x86/setjmp64.o
+cflatobjs += lib/x86/intel-iommu.o
 
 tests = $(TEST_DIR)/access.flat $(TEST_DIR)/apic.flat \
 	  $(TEST_DIR)/emulator.flat $(TEST_DIR)/idt_test.flat \
@@ -14,6 +15,7 @@ tests = $(TEST_DIR)/access.flat $(TEST_DIR)/apic.flat \
 tests += $(TEST_DIR)/svm.flat
 tests += $(TEST_DIR)/vmx.flat
 tests += $(TEST_DIR)/tscdeadline_latency.flat
+tests += $(TEST_DIR)/intel-iommu.flat
 
 include $(TEST_DIR)/Makefile.common
 
x86/intel-iommu.c new file +27 -0
new file mode 100644
index 0000000..f247913
@@ -0,0 +1,27 @@
+/*
+ * Intel IOMMU unit test.
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * Authors:
+ *   Peter Xu <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or
+ * later.
+ */
+
+#include "intel-iommu.h"
+
+int main(int argc, char *argv[])
+{
+	vtd_init();
+
+	report("fault status check", vtd_readl(DMAR_FSTS_REG) == 0);
+	report("QI enablement", vtd_readl(DMAR_GSTS_REG) & VTD_GCMD_QI);
+	report("DMAR table setup", vtd_readl(DMAR_GSTS_REG) & VTD_GCMD_ROOT);
+	report("IR table setup", vtd_readl(DMAR_GSTS_REG) & VTD_GCMD_IR_TABLE);
+	report("DMAR enablement", vtd_readl(DMAR_GSTS_REG) & VTD_GCMD_DMAR);
+	report("IR enablement", vtd_readl(DMAR_GSTS_REG) & VTD_GCMD_IR);
+
+	return report_summary();
+}
x86/unittests.cfg +7 -0
index 23395c6..5413838 100644
@@ -217,3 +217,10 @@ extra_params = -cpu kvm64,hv_time,hv_synic,hv_stimer -device hyperv-testdev
 file = hyperv_clock.flat
 smp = 2
 extra_params = -cpu kvm64,hv_time
+
+[intel_iommu]
+file = intel-iommu.flat
+arch = x86_64
+timeout = 30
+smp = 4
+extra_params = -M q35,kernel-irqchip=split -device intel-iommu,intremap=on,eim=off -device edu
Signature · 1 line
2.7.4
author
#5 lore raw

PATCH v6 5/14 libcflat: add IS_ALIGNED() macro, and page sizes

In reply to Peter Xu

These macros will be useful to do page alignment checks.

---
 lib/libcflat.h | 6 ++++++
 1 file changed, 6 insertions(+)
lib/libcflat.h +6 -0
index 038ea1d..cbce346 100644
@@ -33,6 +33,12 @@
 #define __ALIGN_MASK(x, mask)	(((x) + (mask)) & ~(mask))
 #define __ALIGN(x, a)		__ALIGN_MASK(x, (typeof(x))(a) - 1)
 #define ALIGN(x, a)		__ALIGN((x), (a))
+#define IS_ALIGNED(x, a)	(((x) & ((typeof(x))(a) - 1)) == 0)
+
+#define SZ_4K                  (1 << 12)
+#define SZ_64K                 (1 << 16)
+#define SZ_2M                  (1 << 21)
+#define SZ_1G                  (1 << 30)
 
 typedef uint8_t		u8;
 typedef int8_t		s8;
Signature · 1 line
2.7.4
author
#6 lore raw

PATCH v6 6/14 libcflat: moving MIN/MAX here

In reply to Peter Xu

That's something can be used outside alloc.c.

---
 lib/alloc.c    | 3 ---
 lib/libcflat.h | 3 +++
 2 files changed, 3 insertions(+), 3 deletions(-)
lib/alloc.c +0 -3
index e1d7b8a..58af52b 100644
@@ -7,9 +7,6 @@
 #include "asm/spinlock.h"
 #include "asm/io.h"
 
-#define MIN(a, b)		((a) < (b) ? (a) : (b))
-#define MAX(a, b)		((a) > (b) ? (a) : (b))
-
 #define PHYS_ALLOC_NR_REGIONS	256
 
 struct phys_alloc_region {
lib/libcflat.h +3 -0
index cbce346..f76e983 100644
@@ -40,6 +40,9 @@
 #define SZ_2M                  (1 << 21)
 #define SZ_1G                  (1 << 30)
 
+#define MIN(a, b)		((a) < (b) ? (a) : (b))
+#define MAX(a, b)		((a) > (b) ? (a) : (b))
+
 typedef uint8_t		u8;
 typedef int8_t		s8;
 typedef uint16_t	u16;
Signature · 1 line
2.7.4
author
#7 lore raw

PATCH v6 7/14 vm/page: provide PGDIR_OFFSET() macro

In reply to Peter Xu

This can be used in further patches.

---
 lib/x86/asm/page.h | 3 +++
 lib/x86/vm.c       | 4 ++--
 2 files changed, 5 insertions(+), 2 deletions(-)
lib/x86/asm/page.h +3 -0
index 5044a49..c43bab2 100644
@@ -41,5 +41,8 @@
 #define	PGDIR_MASK	1023
 #endif
 
+#define PGDIR_BITS(lvl)        (((lvl) - 1) * PGDIR_WIDTH + PAGE_SHIFT)
+#define PGDIR_OFFSET(va, lvl)  (((va) >> PGDIR_BITS(lvl)) & PGDIR_MASK)
+
 #endif /* !__ASSEMBLY__ */
 #endif
lib/x86/vm.c +2 -2
index f7e778b..cda4c5f 100644
@@ -48,7 +48,7 @@ unsigned long *install_pte(unsigned long *cr3,
     unsigned offset;
 
     for (level = PAGE_LEVEL; level > pte_level; --level) {
-	offset = ((unsigned long)virt >> ((level-1) * PGDIR_WIDTH + 12)) & PGDIR_MASK;
+	offset = PGDIR_OFFSET((unsigned long)virt, level);
 	if (!(pt[offset] & PT_PRESENT_MASK)) {
 	    unsigned long *new_pt = pt_page;
             if (!new_pt)
@@ -60,7 +60,7 @@ unsigned long *install_pte(unsigned long *cr3,
 	}
 	pt = phys_to_virt(pt[offset] & PT_ADDR_MASK);
     }
-    offset = ((unsigned long)virt >> ((level-1) * PGDIR_WIDTH + 12)) & PGDIR_MASK;
+    offset = PGDIR_OFFSET((unsigned long)virt, level);
     pt[offset] = pte;
     return &pt[offset];
 }
Signature · 1 line
2.7.4
author
#8 lore raw

PATCH v6 8/14 pci: introduce struct pci_dev

In reply to Peter Xu

To extend current PCI framework, we need a per-device struct to store device specific information. Time to have a pci_dev struct. Most of the current PCI APIs are converted to use this pci_dev object as the first argument. Currently it only contains one field "bdf", which is the bdf of current device.

For a few APIs like pci_config_*() ops or pci_find_dev(), I kept the old interface (use PCI BDF value rather than "struct pci_dev") since they can be used in a open context that without any specific PCI device.

---
 lib/pci-host-generic.c | 11 ++++++----
 lib/pci-testdev.c      | 10 +++++----
 lib/pci.c              | 56 ++++++++++++++++++++++++++++++--------------------
 lib/pci.h              | 24 ++++++++++++++--------
 x86/vmexit.c           | 18 ++++++++--------
 5 files changed, 73 insertions(+), 46 deletions(-)
lib/pci-host-generic.c +7 -4
index 6ac0f15..8bad8b1 100644
@@ -211,6 +211,7 @@ static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
 
 bool pci_probe(void)
 {
+	struct pci_dev pci_dev;
 	pcidevaddr_t dev;
 	u8 header;
 	u32 cmd;
@@ -221,10 +222,12 @@ bool pci_probe(void)
 	if (!pci_host_bridge)
 		return false;
 
-	for (dev = 0; dev < 256; dev++) {
+	for (dev = 0; dev < PCI_DEVFN_MAX; dev++) {
 		if (!pci_dev_exists(dev))
 			continue;
 
+		pci_dev_init(&pci_dev, dev);
+
 		/* We are only interested in normal PCI devices */
 		header = pci_config_readb(dev, PCI_HEADER_TYPE);
 		if ((header & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_NORMAL)
@@ -236,15 +239,15 @@ bool pci_probe(void)
 			u64 addr;
 
 			if (pci_alloc_resource(dev, i, &addr)) {
-				pci_bar_set_addr(dev, i, addr);
+				pci_bar_set_addr(&pci_dev, i, addr);
 
-				if (pci_bar_is_memory(dev, i))
+				if (pci_bar_is_memory(&pci_dev, i))
 					cmd |= PCI_COMMAND_MEMORY;
 				else
 					cmd |= PCI_COMMAND_IO;
 			}
 
-			if (pci_bar_is64(dev, i))
+			if (pci_bar_is64(&pci_dev, i))
 				i++;
 		}
 
lib/pci-testdev.c +6 -4
index ad482d3..7d298e6 100644
@@ -163,9 +163,10 @@ static int pci_testdev_all(struct pci_test_dev_hdr *test,
 
 int pci_testdev(void)
 {
+	struct pci_dev pci_dev;
+	pcidevaddr_t dev;
 	phys_addr_t addr;
 	void __iomem *mem, *io;
-	pcidevaddr_t dev;
 	int nr_tests = 0;
 	bool ret;
 
@@ -175,14 +176,15 @@ int pci_testdev(void)
 		       "check QEMU '-device pci-testdev' parameter\n");
 		return -1;
 	}
+	pci_dev_init(&pci_dev, dev);
 
-	ret = pci_bar_is_valid(dev, 0) && pci_bar_is_valid(dev, 1);
+	ret = pci_bar_is_valid(&pci_dev, 0) && pci_bar_is_valid(&pci_dev, 1);
 	assert(ret);
 
-	addr = pci_bar_get_addr(dev, 0);
+	addr = pci_bar_get_addr(&pci_dev, 0);
 	mem = ioremap(addr, PAGE_SIZE);
 
-	addr = pci_bar_get_addr(dev, 1);
+	addr = pci_bar_get_addr(&pci_dev, 1);
 	io = (void *)(unsigned long)addr;
 
 	nr_tests += pci_testdev_all(mem, &pci_testdev_mem_ops);
lib/pci.c +34 -22

148 lines not on this page · raw message

lib/pci.h +16 -8
index 86d6b42..355acd0 100644
@@ -15,6 +15,14 @@ enum {
 	PCIDEVADDR_INVALID = 0xffff,
 };
 
+#define PCI_DEVFN_MAX                   256
+
+struct pci_dev {
+	uint16_t bdf;
+};
+
+extern void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf);
+
 extern bool pci_probe(void);
 extern void pci_print(void);
 extern bool pci_dev_exists(pcidevaddr_t dev);
@@ -32,15 +40,15 @@ extern pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id);
  * It is expected the caller is aware of the device BAR layout and never
  * tries to address the middle of a 64-bit register.
  */
-extern phys_addr_t pci_bar_get_addr(pcidevaddr_t dev, int bar_num);
-extern void pci_bar_set_addr(pcidevaddr_t dev, int bar_num, phys_addr_t addr);
-extern phys_addr_t pci_bar_size(pcidevaddr_t dev, int bar_num);
-extern uint32_t pci_bar_get(pcidevaddr_t dev, int bar_num);
+extern phys_addr_t pci_bar_get_addr(struct pci_dev *dev, int bar_num);
+extern void pci_bar_set_addr(struct pci_dev *dev, int bar_num, phys_addr_t addr);
+extern phys_addr_t pci_bar_size(struct pci_dev *dev, int bar_num);
+extern uint32_t pci_bar_get(struct pci_dev *dev, int bar_num);
 extern uint32_t pci_bar_mask(uint32_t bar);
-extern bool pci_bar_is64(pcidevaddr_t dev, int bar_num);
-extern bool pci_bar_is_memory(pcidevaddr_t dev, int bar_num);
-extern bool pci_bar_is_valid(pcidevaddr_t dev, int bar_num);
-extern void pci_bar_print(pcidevaddr_t dev, int bar_num);
+extern bool pci_bar_is64(struct pci_dev *dev, int bar_num);
+extern bool pci_bar_is_memory(struct pci_dev *dev, int bar_num);
+extern bool pci_bar_is_valid(struct pci_dev *dev, int bar_num);
+extern void pci_bar_print(struct pci_dev *dev, int bar_num);
 extern void pci_dev_print_id(pcidevaddr_t dev);
 
 extern int pci_testdev(void);
x86/vmexit.c +10 -8
index 2d99d5f..63fa070 100644
@@ -372,7 +372,8 @@ int main(int ac, char **av)
 	struct fadt_descriptor_rev1 *fadt;
 	int i;
 	unsigned long membar = 0;
-	pcidevaddr_t pcidev;
+	struct pci_dev pcidev;
+	int ret;
 
 	smp_init();
 	setup_vm();
@@ -385,21 +386,22 @@ int main(int ac, char **av)
 	pm_tmr_blk = fadt->pm_tmr_blk;
 	printf("PM timer port is %x\n", pm_tmr_blk);
 
-	pcidev = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST);
-	if (pcidev != PCIDEVADDR_INVALID) {
+	ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST);
+	if (ret != PCIDEVADDR_INVALID) {
+		pci_dev_init(&pcidev, ret);
 		for (i = 0; i < PCI_TESTDEV_NUM_BARS; i++) {
-			if (!pci_bar_is_valid(pcidev, i)) {
+			if (!pci_bar_is_valid(&pcidev, i)) {
 				continue;
 			}
-			if (pci_bar_is_memory(pcidev, i)) {
-				membar = pci_bar_get_addr(pcidev, i);
+			if (pci_bar_is_memory(&pcidev, i)) {
+				membar = pci_bar_get_addr(&pcidev, i);
 				pci_test.memaddr = ioremap(membar, PAGE_SIZE);
 			} else {
-				pci_test.iobar = pci_bar_get_addr(pcidev, i);
+				pci_test.iobar = pci_bar_get_addr(&pcidev, i);
 			}
 		}
 		printf("pci-testdev at 0x%x membar %lx iobar %x\n",
-		       pcidev, membar, pci_test.iobar);
+		       pcidev.bdf, membar, pci_test.iobar);
 	}
 
 	for (i = 0; i < ARRAY_SIZE(tests); ++i)
Signature · 1 line
2.7.4
author
#9 lore raw

PATCH v6 9/14 pci: provide pci_scan_bars()

In reply to Peter Xu

Let's provide a more general way to scan PCI bars, rather than read the config registers every time.

Then let x86/vmexit.c leverage pci_scan_bars()

---
 lib/pci-host-generic.c |  2 +-
 lib/pci.c              | 17 ++++++++++++++++-
 lib/pci.h              |  5 +++++
 x86/vmexit.c           | 17 ++++++-----------
 4 files changed, 28 insertions(+), 13 deletions(-)
lib/pci-host-generic.c +1 -1
index 8bad8b1..ad44031 100644
@@ -235,7 +235,7 @@ bool pci_probe(void)
 
 		cmd = PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
 
-		for (i = 0; i < 6; i++) {
+		for (i = 0; i < PCI_BAR_NUM; i++) {
 			u64 addr;
 
 			if (pci_alloc_resource(dev, i, &addr)) {
lib/pci.c +16 -1
index 8f2356d..d4521be 100644
@@ -207,7 +207,7 @@ static void pci_dev_print(pcidevaddr_t dev)
 	if ((header & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_NORMAL)
 		return;
 
-	for (i = 0; i < 6; i++) {
+	for (i = 0; i < PCI_BAR_NUM; i++) {
 		if (pci_bar_size(&pci_dev, i)) {
 			printf("\t");
 			pci_bar_print(&pci_dev, i);
@@ -227,3 +227,18 @@ void pci_print(void)
 			pci_dev_print(dev);
 	}
 }
+
+void pci_scan_bars(struct pci_dev *dev)
+{
+	int i;
+
+	for (i = 0; i < PCI_BAR_NUM; i++) {
+		if (!pci_bar_is_valid(dev, i))
+			continue;
+		dev->resource[i] = pci_bar_get_addr(dev, i);
+		if (pci_bar_is64(dev, i)) {
+			i++;
+			dev->resource[i] = (phys_addr_t)0;
+		}
+	}
+}
lib/pci.h +5 -0
index 355acd0..8215c9d 100644
@@ -15,13 +15,16 @@ enum {
 	PCIDEVADDR_INVALID = 0xffff,
 };
 
+#define PCI_BAR_NUM                     6
 #define PCI_DEVFN_MAX                   256
 
 struct pci_dev {
 	uint16_t bdf;
+	phys_addr_t resource[PCI_BAR_NUM];
 };
 
 extern void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf);
+extern void pci_scan_bars(struct pci_dev *dev);
 
 extern bool pci_probe(void);
 extern void pci_print(void);
@@ -65,6 +68,8 @@ extern int pci_testdev(void);
  * pci-testdev supports at least three types of tests (via mmio and
  * portio BARs): no-eventfd, wildcard-eventfd and datamatch-eventfd
  */
+#define PCI_TESTDEV_BAR_MEM		0
+#define PCI_TESTDEV_BAR_IO		1
 #define PCI_TESTDEV_NUM_BARS		2
 #define PCI_TESTDEV_NUM_TESTS		3
 
x86/vmexit.c +6 -11
index 63fa070..f9a22de 100644
@@ -389,17 +389,12 @@ int main(int ac, char **av)
 	ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST);
 	if (ret != PCIDEVADDR_INVALID) {
 		pci_dev_init(&pcidev, ret);
-		for (i = 0; i < PCI_TESTDEV_NUM_BARS; i++) {
-			if (!pci_bar_is_valid(&pcidev, i)) {
-				continue;
-			}
-			if (pci_bar_is_memory(&pcidev, i)) {
-				membar = pci_bar_get_addr(&pcidev, i);
-				pci_test.memaddr = ioremap(membar, PAGE_SIZE);
-			} else {
-				pci_test.iobar = pci_bar_get_addr(&pcidev, i);
-			}
-		}
+		pci_scan_bars(&pcidev);
+		assert(pci_bar_is_memory(&pcidev, PCI_TESTDEV_BAR_MEM));
+		assert(!pci_bar_is_memory(&pcidev, PCI_TESTDEV_BAR_IO));
+		membar = pcidev.resource[PCI_TESTDEV_BAR_MEM];
+		pci_test.memaddr = ioremap(membar, PAGE_SIZE);
+		pci_test.iobar = pcidev.resource[PCI_TESTDEV_BAR_IO];
 		printf("pci-testdev at 0x%x membar %lx iobar %x\n",
 		       pcidev.bdf, membar, pci_test.iobar);
 	}
Signature · 1 line
2.7.4
author
#10 lore raw

PATCH v6 10/14 pci: provide pci_enable_defaults()

In reply to Peter Xu

One helper function to set/clear specific bit in PCI_COMMAND register is introduced. Then, provide a function to do most of the common PCI init work.

---
 lib/pci.c | 19 +++++++++++++++++++
 lib/pci.h |  2 ++
 2 files changed, 21 insertions(+)
lib/pci.c +19 -0
index d4521be..bf2a3df 100644
@@ -7,6 +7,18 @@
 #include "pci.h"
 #include "asm/pci.h"
 
+void pci_cmd_set_clr(struct pci_dev *dev, uint16_t set, uint16_t clr)
+{
+	uint16_t val = pci_config_readw(dev->bdf, PCI_COMMAND);
+
+	/* No overlap is allowed */
+	assert((set & clr) == 0);
+	val |= set;
+	val &= ~clr;
+
+	pci_config_writew(dev->bdf, PCI_COMMAND, val);
+}
+
 bool pci_dev_exists(pcidevaddr_t dev)
 {
 	return (pci_config_readw(dev, PCI_VENDOR_ID) != 0xffff &&
@@ -242,3 +254,10 @@ void pci_scan_bars(struct pci_dev *dev)
 		}
 	}
 }
+
+void pci_enable_defaults(struct pci_dev *dev)
+{
+	pci_scan_bars(dev);
+	/* Enable device DMA operations */
+	pci_cmd_set_clr(dev, PCI_COMMAND_MASTER, 0);
+}
lib/pci.h +2 -0
index 8215c9d..d3052ef 100644
@@ -25,6 +25,8 @@ struct pci_dev {
 
 extern void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf);
 extern void pci_scan_bars(struct pci_dev *dev);
+extern void pci_cmd_set_clr(struct pci_dev *dev, uint16_t set, uint16_t clr);
+extern void pci_enable_defaults(struct pci_dev *dev);
 
 extern bool pci_probe(void);
 extern void pci_print(void);
Signature · 1 line
2.7.4
author
#11 lore raw

PATCH v6 11/14 pci: edu: introduce pci-edu helpers

In reply to Peter Xu

QEMU edu device is a pci device that is originally written for educational purpose, however it also suits for IOMMU unit test. Adding helpers for this specific device to implement the device logic.

The device supports lots of functions, here only DMA operation is supported.

The spec of the device can be found at:

https://github.com/qemu/qemu/blob/master/docs/specs/edu.txt

---
 lib/pci-edu.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/pci-edu.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 155 insertions(+)
 create mode 100644 lib/pci-edu.c
 create mode 100644 lib/pci-edu.h
lib/pci-edu.c new file +73 -0
new file mode 100644
index 0000000..c9a8b0c
@@ -0,0 +1,73 @@
+/*
+ * Edu PCI device.
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * Authors:
+ *   Peter Xu <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or
+ * later.
+ */
+
+#include "pci-edu.h"
+#include "asm/barrier.h"
+
+/* Return true if alive */
+static inline bool edu_check_alive(struct pci_edu_dev *dev)
+{
+	static uint32_t live_count = 1;
+	uint32_t value;
+
+	edu_reg_writel(dev, EDU_REG_ALIVE, live_count++);
+	value = edu_reg_readl(dev, EDU_REG_ALIVE);
+	return (live_count - 1 == ~value);
+}
+
+bool edu_init(struct pci_edu_dev *dev)
+{
+	pcidevaddr_t dev_addr;
+
+	dev_addr = pci_find_dev(PCI_VENDOR_ID_QEMU, PCI_DEVICE_ID_EDU);
+	if (dev_addr == PCIDEVADDR_INVALID)
+		return false;
+
+	pci_dev_init(&dev->pci_dev, dev_addr);
+	pci_enable_defaults(&dev->pci_dev);
+	assert(edu_check_alive(dev));
+	return true;
+}
+
+void edu_dma(struct pci_edu_dev *dev, iova_t iova,
+	     size_t size, int dev_offset, bool from_device)
+{
+	uint64_t from, to;
+	uint32_t cmd = EDU_CMD_DMA_START;
+
+	assert(size <= EDU_DMA_SIZE_MAX);
+	assert(dev_offset < EDU_DMA_SIZE_MAX &&
+	       dev_offset >= 0);
+
+	printf("edu device DMA start %s addr %p size 0x%lu off 0x%x\n",
+	       from_device ? "FROM" : "TO",
+	       (void *)iova, size, dev_offset);
+
+	if (from_device) {
+		from = dev_offset + EDU_DMA_START;
+		to = iova;
+		cmd |= EDU_CMD_DMA_FROM;
+	} else {
+		from = iova;
+		to = EDU_DMA_START + dev_offset;
+		cmd |= EDU_CMD_DMA_TO;
+	}
+
+	edu_reg_writeq(dev, EDU_REG_DMA_SRC, from);
+	edu_reg_writeq(dev, EDU_REG_DMA_DST, to);
+	edu_reg_writeq(dev, EDU_REG_DMA_COUNT, size);
+	edu_reg_writel(dev, EDU_REG_DMA_CMD, cmd);
+
+	/* Wait until DMA finished */
+	while (edu_reg_readl(dev, EDU_REG_DMA_CMD) & EDU_CMD_DMA_START)
+		cpu_relax();
+}
lib/pci-edu.h new file +82 -0

82 lines not on this page · raw message

Signature · 1 line
2.7.4
#12 lore raw

Re: [PATCH kvm-unit-tests v6 11/14] pci: edu: introduce pci-edu helpers

Quoted 171 lines · Peter Xu

QEMU edu device is a pci device that is originally written for educational purpose, however it also suits for IOMMU unit test. Adding helpers for this specific device to implement the device logic.

The device supports lots of functions, here only DMA operation is supported.

The spec of the device can be found at:

https://github.com/qemu/qemu/blob/master/docs/specs/edu.txt

---
 lib/pci-edu.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/pci-edu.h | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 155 insertions(+)
 create mode 100644 lib/pci-edu.c
 create mode 100644 lib/pci-edu.h
lib/pci-edu.c new file +73 -0
new file mode 100644
index 0000000..c9a8b0c
@@ -0,0 +1,73 @@
+/*
+ * Edu PCI device.
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * Authors:
+ *   Peter Xu <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or
+ * later.
+ */
+
+#include "pci-edu.h"
+#include "asm/barrier.h"
+
+/* Return true if alive */
+static inline bool edu_check_alive(struct pci_edu_dev *dev)
+{
+	static uint32_t live_count = 1;
+	uint32_t value;
+
+	edu_reg_writel(dev, EDU_REG_ALIVE, live_count++);
+	value = edu_reg_readl(dev, EDU_REG_ALIVE);
+	return (live_count - 1 == ~value);
+}
+
+bool edu_init(struct pci_edu_dev *dev)
+{
+	pcidevaddr_t dev_addr;
+
+	dev_addr = pci_find_dev(PCI_VENDOR_ID_QEMU, PCI_DEVICE_ID_EDU);
+	if (dev_addr == PCIDEVADDR_INVALID)
+		return false;
+
+	pci_dev_init(&dev->pci_dev, dev_addr);
+	pci_enable_defaults(&dev->pci_dev);
+	assert(edu_check_alive(dev));
+	return true;
+}
+
+void edu_dma(struct pci_edu_dev *dev, iova_t iova,
+	     size_t size, int dev_offset, bool from_device)
+{
+	uint64_t from, to;
+	uint32_t cmd = EDU_CMD_DMA_START;
+
+	assert(size <= EDU_DMA_SIZE_MAX);
+	assert(dev_offset < EDU_DMA_SIZE_MAX &&
+	       dev_offset >= 0);
+
+	printf("edu device DMA start %s addr %p size 0x%lu off 0x%x\n",
+	       from_device ? "FROM" : "TO",
+	       (void *)iova, size, dev_offset);
+
+	if (from_device) {
+		from = dev_offset + EDU_DMA_START;
+		to = iova;
+		cmd |= EDU_CMD_DMA_FROM;
+	} else {
+		from = iova;
+		to = EDU_DMA_START + dev_offset;
+		cmd |= EDU_CMD_DMA_TO;
+	}
+
+	edu_reg_writeq(dev, EDU_REG_DMA_SRC, from);
+	edu_reg_writeq(dev, EDU_REG_DMA_DST, to);
+	edu_reg_writeq(dev, EDU_REG_DMA_COUNT, size);
+	edu_reg_writel(dev, EDU_REG_DMA_CMD, cmd);
+
+	/* Wait until DMA finished */
+	while (edu_reg_readl(dev, EDU_REG_DMA_CMD) & EDU_CMD_DMA_START)
+		cpu_relax();
+}
lib/pci-edu.h new file +66 -0
new file mode 100644
index 0000000..5d896c9
@@ -0,0 +1,82 @@
+/*
+ * Edu PCI device header.
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * Authors:
+ *   Peter Xu <[email protected]>,
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or
+ * later.
+ *
+ * Edu device is a virtualized device in QEMU. Please refer to
+ * docs/specs/edu.txt in QEMU repository for EDU device manual.
+ */
+#ifndef __PCI_EDU_H__
+#define __PCI_EDU_H__
+
+#include "pci.h"
+#include "asm/io.h"
+
+#define  PCI_VENDOR_ID_QEMU              0x1234
+#define  PCI_DEVICE_ID_EDU               0x11e8
+
+/* The only bar used by EDU device */
+#define EDU_BAR                     0
+#define EDU_MAGIC                   0xed
+#define EDU_VERSION                 0x100
+#define EDU_DMA_BUF_SIZE            (1 << 20)
+#define EDU_INPUT_BUF_SIZE          256
+
+#define EDU_REG_ID                  0x0
+#define EDU_REG_ALIVE               0x4
+#define EDU_REG_FACTORIAL           0x8
+#define EDU_REG_STATUS              0x20
+#define EDU_REG_DMA_SRC             0x80
+#define EDU_REG_DMA_DST             0x88
+#define EDU_REG_DMA_COUNT           0x90
+#define EDU_REG_DMA_CMD             0x98
+
+#define EDU_CMD_DMA_START           0x01
+#define EDU_CMD_DMA_FROM            0x02
+#define EDU_CMD_DMA_TO              0x00
+
+#define EDU_STATUS_FACTORIAL        0x1
+#define EDU_STATUS_INT_ENABLE       0x80
+
+#define EDU_DMA_START               0x40000
+#define EDU_DMA_SIZE_MAX            4096
+
+struct pci_edu_dev {
+	struct pci_dev pci_dev;
+};
+
+#define edu_reg(d, r) (volatile void *)((d)->pci_dev.resource[EDU_BAR] + (r))
+
+static inline uint64_t edu_reg_readq(struct pci_edu_dev *dev, int reg)
+{
+	return __raw_readq(edu_reg(dev, reg));
+}
+
+static inline uint32_t edu_reg_readl(struct pci_edu_dev *dev, int reg)
+{
+	return __raw_readl(edu_reg(dev, reg));
+}
+
+static inline void edu_reg_writeq(struct pci_edu_dev *dev, int reg,

lib/pci-edu.h line 76

Quoted 10 lines
lib/pci-edu.h new file +10 -0
+				  uint64_t val)
+{
+	__raw_writeq(val, edu_reg(dev, reg));
+}
+
+static inline void edu_reg_writel(struct pci_edu_dev *dev, int reg,
+				  uint32_t val)
+{
+	__raw_writel(val, edu_reg(dev, reg));
+}

Can you refresh my memory on why we're using __raw accessors here instead of the __raw wrappers that include barriers and endian conversion? Hmm, actually it doesn't look like the EDU device specifies an endianness...

Quoted 13 lines
lib/pci-edu.h new file +6 -0
+
+bool edu_init(struct pci_edu_dev *dev);
+void edu_dma(struct pci_edu_dev *dev, iova_t iova,
+	     size_t size, int dev_offset, bool from_device);
+
+#endif
Signature · 6 lines
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#13 lore raw

Re: [PATCH kvm-unit-tests v6 11/14] pci: edu: introduce pci-edu helpers

On Thu, Nov 24, 2016 at 09:47:01AM +0100, Andrew Jones wrote:

[...]

Quoted 17 lines
Quoted 17 lines
lib/pci-edu.h new file +17 -0
+static inline uint64_t edu_reg_readq(struct pci_edu_dev *dev, int reg)
+{
+	return __raw_readq(edu_reg(dev, reg));
+}
+
+static inline uint32_t edu_reg_readl(struct pci_edu_dev *dev, int reg)
+{
+	return __raw_readl(edu_reg(dev, reg));
+}
+
+static inline void edu_reg_writeq(struct pci_edu_dev *dev, int reg,
+				  uint64_t val)
+{
+	__raw_writeq(val, edu_reg(dev, reg));
+}
+
+static inline void edu_reg_writel(struct pci_edu_dev *dev, int reg,
Quoted 8 lines

lib/pci-edu.h line 76

Quoted 4 lines
lib/pci-edu.h new file +4 -0
+				  uint32_t val)
+{
+	__raw_writel(val, edu_reg(dev, reg));
+}

Can you refresh my memory on why we're using __raw accessors here instead of the __raw wrappers that include barriers and endian conversion? Hmm, actually it doesn't look like the EDU device specifies an endianness...

I see that __raw wrappers (read[bwlq]) are assuming the data is little endian, while edu device is configured as native endianess (DEVICE_NATIVE_ENDIAN). So looks like using __raw accessors should be the correct way?

But, yes here I missed memory barriers. I have only volatile protections (which __raw_*() provides).

Thanks,

-- peterx

#14 lore raw

Re: [PATCH kvm-unit-tests v6 11/14] pci: edu: introduce pci-edu helpers

Quoted 30 lines · Peter Xu

On Thu, Nov 24, 2016 at 09:47:01AM +0100, Andrew Jones wrote:

[...]

Quoted 17 lines
Quoted 17 lines
lib/pci-edu.h new file +17 -0
+static inline uint64_t edu_reg_readq(struct pci_edu_dev *dev, int reg)
+{
+	return __raw_readq(edu_reg(dev, reg));
+}
+
+static inline uint32_t edu_reg_readl(struct pci_edu_dev *dev, int reg)
+{
+	return __raw_readl(edu_reg(dev, reg));
+}
+
+static inline void edu_reg_writeq(struct pci_edu_dev *dev, int reg,
+				  uint64_t val)
+{
+	__raw_writeq(val, edu_reg(dev, reg));
+}
+
+static inline void edu_reg_writel(struct pci_edu_dev *dev, int reg,
Quoted 8 lines

lib/pci-edu.h line 76

Quoted 4 lines
lib/pci-edu.h new file +4 -0
+				  uint32_t val)
+{
+	__raw_writel(val, edu_reg(dev, reg));
+}

Can you refresh my memory on why we're using __raw accessors here instead of the __raw wrappers that include barriers and endian conversion? Hmm, actually it doesn't look like the EDU device specifies an endianness...

Quoted 4 lines

I see that __raw wrappers (read[bwlq]) are assuming the data is little endian, while edu device is configured as native endianess (DEVICE_NATIVE_ENDIAN). So looks like using __raw accessors should be the correct way?

Yeah, I guess that's correct. Native endian...bleh.

Quoted 11 lines

But, yes here I missed memory barriers. I have only volatile protections (which __raw_*() provides).

Thanks,

-- peterx
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw

Re: [PATCH kvm-unit-tests v6 11/14] pci: edu: introduce pci-edu helpers

In reply to Peter Xu

lib/pci-edu.c line 37

Quoted 11 lines · Peter Xu
lib/pci-edu.c new file +11 -0
+bool edu_init(struct pci_edu_dev *dev)
+{
+	pcidevaddr_t dev_addr;
+
+	dev_addr = pci_find_dev(PCI_VENDOR_ID_QEMU, PCI_DEVICE_ID_EDU);
+	if (dev_addr == PCIDEVADDR_INVALID)
+		return false;
+
+	pci_dev_init(&dev->pci_dev, dev_addr);
+	pci_enable_defaults(&dev->pci_dev);
+	assert(edu_check_alive(dev));

It is rather a question to Andrew.
Were not you opposed to placing function calls into asserts?

Thanks!

Quoted 2 lines
(quoted diff) +2 -0
+	return true;
+}
#16 lore raw

Re: [PATCH kvm-unit-tests v6 11/14] pci: edu: introduce pci-edu helpers

Quoted 15 lines · Alexander Gordeev

lib/pci-edu.c line 37

Quoted 11 lines · Peter Xu
lib/pci-edu.c new file +11 -0
+bool edu_init(struct pci_edu_dev *dev)
+{
+	pcidevaddr_t dev_addr;
+
+	dev_addr = pci_find_dev(PCI_VENDOR_ID_QEMU, PCI_DEVICE_ID_EDU);
+	if (dev_addr == PCIDEVADDR_INVALID)
+		return false;
+
+	pci_dev_init(&dev->pci_dev, dev_addr);
+	pci_enable_defaults(&dev->pci_dev);
+	assert(edu_check_alive(dev));

It is rather a question to Andrew.
Were not you opposed to placing function calls into asserts?

Opposed to code we want without asserts enabled being placed in asserts. If edu_check_alive is only here for the assert, then it doesn't matter. If edu_check_alive has some side-effect, even a printf, then it should be changed to ret = edu_check_alive, assert(ret). Allowing us to change assert() into a no-op at no loss of functionality.

Thanks,
drew

Quoted 9 lines

Thanks!

Review

Quoted 2 lines
(quoted diff) +2 -0
+	return true;
+}

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#17 lore raw

Re: [PATCH kvm-unit-tests v6 11/14] pci: edu: introduce pci-edu helpers

In reply to Peter Xu

lib/pci-edu.c line 49

Quoted 9 lines · Peter Xu
lib/pci-edu.c new file +9 -0
+void edu_dma(struct pci_edu_dev *dev, iova_t iova,
+	     size_t size, int dev_offset, bool from_device)
+{
+	uint64_t from, to;
+	uint32_t cmd = EDU_CMD_DMA_START;
+
+	assert(size <= EDU_DMA_SIZE_MAX);
+	assert(dev_offset < EDU_DMA_SIZE_MAX &&
+	       dev_offset >= 0);

Any reason for dev_offset not to be unsigned?

Quoted 23 lines
lib/pci-edu.c new file +23 -0
+	printf("edu device DMA start %s addr %p size 0x%lu off 0x%x\n",
+	       from_device ? "FROM" : "TO",
+	       (void *)iova, size, dev_offset);
+
+	if (from_device) {
+		from = dev_offset + EDU_DMA_START;
+		to = iova;
+		cmd |= EDU_CMD_DMA_FROM;
+	} else {
+		from = iova;
+		to = EDU_DMA_START + dev_offset;
+		cmd |= EDU_CMD_DMA_TO;
+	}
+
+	edu_reg_writeq(dev, EDU_REG_DMA_SRC, from);
+	edu_reg_writeq(dev, EDU_REG_DMA_DST, to);
+	edu_reg_writeq(dev, EDU_REG_DMA_COUNT, size);
+	edu_reg_writel(dev, EDU_REG_DMA_CMD, cmd);
+
+	/* Wait until DMA finished */
+	while (edu_reg_readl(dev, EDU_REG_DMA_CMD) & EDU_CMD_DMA_START)
+		cpu_relax();
+}
author
#18 lore raw

Re: [PATCH kvm-unit-tests v6 11/14] pci: edu: introduce pci-edu helpers

Quoted 12 lines · Alexander Gordeev

lib/pci-edu.c line 49

Quoted 9 lines · Peter Xu
lib/pci-edu.c new file +9 -0
+void edu_dma(struct pci_edu_dev *dev, iova_t iova,
+	     size_t size, int dev_offset, bool from_device)
+{
+	uint64_t from, to;
+	uint32_t cmd = EDU_CMD_DMA_START;
+
+	assert(size <= EDU_DMA_SIZE_MAX);
+	assert(dev_offset < EDU_DMA_SIZE_MAX &&
+	       dev_offset >= 0);

Any reason for dev_offset not to be unsigned?

No specific reason (though logically it can be <0 value, since it'll be by default shifted by EDU_DMA_START later). I can switch to unsigned if there is any further repost. Thanks,

-- peterx

author
#19 lore raw

PATCH v6 12/14 x86: intel-iommu: add dmar test

In reply to Peter Xu

DMAR test is based on QEMU edu device. A 4B DMA memory copy is carried out as the simplest DMAR test.

---
 lib/pci.h             |   5 ++
 lib/x86/intel-iommu.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/intel-iommu.h |  23 +++++++++
 x86/Makefile.common   |   1 +
 x86/intel-iommu.c     |  51 +++++++++++++++++++
 5 files changed, 212 insertions(+)
lib/pci.h +5 -0
index d3052ef..26968b1 100644
@@ -18,6 +18,9 @@ enum {
 #define PCI_BAR_NUM                     6
 #define PCI_DEVFN_MAX                   256
 
+#define PCI_BDF_GET_DEVFN(x)            ((x) & 0xff)
+#define PCI_BDF_GET_BUS(x)              (((x) >> 8) & 0xff)
+
 struct pci_dev {
 	uint16_t bdf;
 	phys_addr_t resource[PCI_BAR_NUM];
@@ -28,6 +31,8 @@ extern void pci_scan_bars(struct pci_dev *dev);
 extern void pci_cmd_set_clr(struct pci_dev *dev, uint16_t set, uint16_t clr);
 extern void pci_enable_defaults(struct pci_dev *dev);
 
+typedef phys_addr_t iova_t;
+
 extern bool pci_probe(void);
 extern void pci_print(void);
 extern bool pci_dev_exists(pcidevaddr_t dev);
lib/x86/intel-iommu.c +132 -0

144 lines not on this page · raw message

lib/x86/intel-iommu.h +23 -0
index fae9ae5..1352639 100644
@@ -20,6 +20,7 @@
 #include "isr.h"
 #include "smp.h"
 #include "desc.h"
+#include "pci.h"
 #include "asm/io.h"
 
 #define Q35_HOST_BRIDGE_IOMMU_ADDR  0xfed90000ULL
@@ -91,6 +92,27 @@
 #define VTD_GCMD_ONE_SHOT_BITS  (VTD_GCMD_IR_TABLE | VTD_GCMD_WBF | \
 				 VTD_GCMD_SFL | VTD_GCMD_ROOT)
 
+/* Supported Adjusted Guest Address Widths */
+#define VTD_CAP_SAGAW_SHIFT         8
+/* 39-bit AGAW, 3-level page-table */
+#define VTD_CAP_SAGAW_39bit         (0x2ULL << VTD_CAP_SAGAW_SHIFT)
+/* 48-bit AGAW, 4-level page-table */
+#define VTD_CAP_SAGAW_48bit         (0x4ULL << VTD_CAP_SAGAW_SHIFT)
+#define VTD_CAP_SAGAW               VTD_CAP_SAGAW_39bit
+
+/* Both 1G/2M huge pages */
+#define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
+
+#define VTD_CONTEXT_TT_MULTI_LEVEL  0
+#define VTD_CONTEXT_TT_DEV_IOTLB    1
+#define VTD_CONTEXT_TT_PASS_THROUGH 2
+
+#define VTD_PTE_R                   (1 << 0)
+#define VTD_PTE_W                   (1 << 1)
+#define VTD_PTE_RW                  (VTD_PTE_R | VTD_PTE_W)
+#define VTD_PTE_ADDR                GENMASK_ULL(51, 12)
+#define VTD_PTE_HUGE                (1 << 7)
+
 #define vtd_reg(reg) ((volatile void *)(Q35_HOST_BRIDGE_IOMMU_ADDR + reg))
 
 static inline void vtd_writel(unsigned int reg, uint32_t value)
@@ -114,5 +136,6 @@ static inline uint64_t vtd_readq(unsigned int reg)
 }
 
 void vtd_init(void);
+void vtd_map_range(uint16_t sid, phys_addr_t iova, phys_addr_t pa, size_t size);
 
 #endif
x86/Makefile.common +1 -0
index 356d879..1dad18b 100644
@@ -3,6 +3,7 @@
 all: test_cases
 
 cflatobjs += lib/pci.o
+cflatobjs += lib/pci-edu.o
 cflatobjs += lib/x86/io.o
 cflatobjs += lib/x86/smp.o
 cflatobjs += lib/x86/vm.o
x86/intel-iommu.c +51 -0

66 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#20 lore raw

PATCH v6 13/14 pci: add msi support for 32/64bit address

In reply to Peter Xu

pci_cap_walk() is provided to allow walk through all the capability bits for a specific PCI device. If a cap handler is provided, it'll be triggered if the cap is detected along the cap walk.

MSI cap handler is the first one supported. We can add more cap handler in the future.

Meanwhile, pci_setup_msi() API is provided to support basic 32/64 bit address MSI setup.

---
 lib/pci.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/pci.h |  4 ++++
 2 files changed, 73 insertions(+)
lib/pci.c +69 -0
index bf2a3df..a51eb8e 100644
@@ -7,6 +7,74 @@
 #include "pci.h"
 #include "asm/pci.h"
 
+typedef void (*pci_cap_handler)(struct pci_dev *dev, int cap_offset);
+
+static void pci_cap_msi_handler(struct pci_dev *dev, int cap_offset)
+{
+	printf("Detected MSI for device 0x%x offset 0x%x\n",
+	       dev->bdf, cap_offset);
+	dev->msi_offset = cap_offset;
+}
+
+static pci_cap_handler cap_handlers[PCI_CAP_ID_MAX + 1] = {
+	[PCI_CAP_ID_MSI] = pci_cap_msi_handler,
+};
+
+void pci_cap_walk(struct pci_dev *dev)
+{
+	uint8_t cap_offset;
+	uint8_t cap_id;
+	int count = 0;
+
+	cap_offset = pci_config_readb(dev->bdf, PCI_CAPABILITY_LIST);
+	while (cap_offset) {
+		cap_id = pci_config_readb(dev->bdf, cap_offset);
+		printf("PCI detected cap 0x%x\n", cap_id);
+		assert(cap_id < PCI_CAP_ID_MAX + 1);
+		if (cap_handlers[cap_id])
+			cap_handlers[cap_id](dev, cap_offset);
+		cap_offset = pci_config_readb(dev->bdf, cap_offset + 1);
+		/* Avoid dead loop during cap walk */
+		assert(++count <= 255);
+	}
+}
+
+bool pci_setup_msi(struct pci_dev *dev, uint64_t msi_addr, uint32_t msi_data)
+{
+	uint16_t msi_control;
+	uint16_t offset;
+	pcidevaddr_t addr;
+
+	assert(dev);
+
+	if (!dev->msi_offset) {
+		printf("MSI: dev 0x%x does not support MSI.\n", dev->bdf);
+		return false;
+	}
+
+	addr = dev->bdf;
+	offset = dev->msi_offset;
+	msi_control = pci_config_readw(addr, offset + PCI_MSI_FLAGS);
+	pci_config_writel(addr, offset + PCI_MSI_ADDRESS_LO,
+			  msi_addr & 0xffffffff);
+
+	if (msi_control & PCI_MSI_FLAGS_64BIT) {
+		pci_config_writel(addr, offset + PCI_MSI_ADDRESS_HI,
+				  (uint32_t)(msi_addr >> 32));
+		pci_config_writel(addr, offset + PCI_MSI_DATA_64, msi_data);
+		printf("MSI: dev 0x%x init 64bit address: ", addr);
+	} else {
+		pci_config_writel(addr, offset + PCI_MSI_DATA_32, msi_data);
+		printf("MSI: dev 0x%x init 32bit address: ", addr);
+	}
+	printf("addr=0x%lx, data=0x%x\n", msi_addr, msi_data);
+
+	msi_control |= PCI_MSI_FLAGS_ENABLE;
+	pci_config_writew(addr, offset + PCI_MSI_FLAGS, msi_control);
+
+	return true;
+}
+
 void pci_cmd_set_clr(struct pci_dev *dev, uint16_t set, uint16_t clr)
 {
 	uint16_t val = pci_config_readw(dev->bdf, PCI_COMMAND);
@@ -260,4 +328,5 @@ void pci_enable_defaults(struct pci_dev *dev)
 	pci_scan_bars(dev);
 	/* Enable device DMA operations */
 	pci_cmd_set_clr(dev, PCI_COMMAND_MASTER, 0);
+	pci_cap_walk(dev);
 }
lib/pci.h +4 -0
index 26968b1..e71a05d 100644
@@ -23,13 +23,17 @@ enum {
 
 struct pci_dev {
 	uint16_t bdf;
+	uint16_t msi_offset;
 	phys_addr_t resource[PCI_BAR_NUM];
 };
 
 extern void pci_dev_init(struct pci_dev *dev, pcidevaddr_t bdf);
 extern void pci_scan_bars(struct pci_dev *dev);
 extern void pci_cmd_set_clr(struct pci_dev *dev, uint16_t set, uint16_t clr);
+extern void pci_cap_walk(struct pci_dev *dev);
 extern void pci_enable_defaults(struct pci_dev *dev);
+extern bool pci_setup_msi(struct pci_dev *dev, uint64_t msi_addr,
+			  uint32_t msi_data);
 
 typedef phys_addr_t iova_t;
 
Signature · 1 line
2.7.4
Reviewed-by on v6 13/14
#21 lore raw

Re: [PATCH kvm-unit-tests v6 13/14] pci: add msi support for 32/64bit address

Quoted 7 lines · Peter Xu

pci_cap_walk() is provided to allow walk through all the capability bits for a specific PCI device. If a cap handler is provided, it'll be triggered if the cap is detected along the cap walk.

MSI cap handler is the first one supported. We can add more cap handler in the future.

Quoted 8 lines

Meanwhile, pci_setup_msi() API is provided to support basic 32/64 bit address MSI setup.

---
 lib/pci.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/pci.h |  4 ++++
 2 files changed, 73 insertions(+)
author
#22 lore raw

PATCH v6 14/14 x86: intel-iommu: add IR MSI test

In reply to Peter Xu

First of all, vtd_setup_msi() is provided. It setup IRTE entries, meanwhile, setup PCI device MSI vectors corresponding to VT-d spec.

Meanwhile, IR MSI test is added to intel IOMMU unit test. The basic IR test is carried out by a edu INTR raise request. When write to the intr raise register, interrupt will be generated. Type of interrupt will depend on the setup (either INTx or MSI).

---
 lib/pci-edu.h         |  1 +
 lib/x86/intel-iommu.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/intel-iommu.h |  1 +
 x86/intel-iommu.c     | 44 +++++++++++++++++++++++-
 4 files changed, 139 insertions(+), 1 deletion(-)
lib/pci-edu.h +1 -0
index 5d896c9..dc08d2e 100644
@@ -32,6 +32,7 @@
 #define EDU_REG_ALIVE               0x4
 #define EDU_REG_FACTORIAL           0x8
 #define EDU_REG_STATUS              0x20
+#define EDU_REG_INTR_RAISE          0x60
 #define EDU_REG_DMA_SRC             0x80
 #define EDU_REG_DMA_DST             0x88
 #define EDU_REG_DMA_COUNT           0x90
lib/x86/intel-iommu.c +94 -0

112 lines not on this page · raw message

lib/x86/intel-iommu.h +1 -0
index 1352639..13feafa 100644
@@ -137,5 +137,6 @@ static inline uint64_t vtd_readq(unsigned int reg)
 
 void vtd_init(void);
 void vtd_map_range(uint16_t sid, phys_addr_t iova, phys_addr_t pa, size_t size);
+bool vtd_setup_msi(struct pci_dev *dev, int vector, int dest_id);
 
 #endif
x86/intel-iommu.c +43 -1

65 lines not on this page · raw message

Signature · 1 line
2.7.4
#23 lore raw
In reply to Peter Xu
Quoted 8 lines · Peter Xu

This is v6 of vt-d unit test series.

Patch "libcflat: add IS_ALIGNED() macro, and page sizes" is picked up by Drew in the ARM GIC framework series, so please feel free to drop it when needed.

Online repo:

Quoted 8 lines

https://github.com/xzpeter/kvm-unit-tests.git iommu-ut-v6

Please review. Thanks,

v6:
- pci: rename pci_dev.bar to pcidev.resource (to Drew: I didn't remove your r-b in several patches due to this change, assuming the change is trivial) [Alex]

No problem. Actually, you should have left my r-b on the cap-walk patch too, as adding a couple asserts is also trivial.

drew

Quoted 148 lines

- pci_cap_walk(): add an assert to avoid dead loop [Alex]
- pci_cap_walk(): another assertion to make sure cap_id is valid [Drew]

v5:
- add one more patch at the beginning of series to add "extern" to pci_testdev() header definition [Drew]
- add "extern" to all pci.h functions [Drew]
- update IS_ALIGNED patch to be aligned with Drew's gic series [Drew]
- replace one more PCI_DEVFN_MAX/PCI_BAR_NUM in lib/pci-host-generic.c [Drew]
- add {} in vtd_install_pte() when there are one line code and one line comment [Drew]
- drop Drew's suggested-by in patch "x86: intel-iommu: add IR MSI test" (I forgot why I added it, but anyway let me drop it here...) [Drew]

v4:
(same as v3, but with correct patches sent out)

v3:
- is_power_of_2(): remove outer "()" [Drew]
- fix indent for vtd_root_table() [Drew]
- pci_find_dev(): keep {} to avoid touching more codes [Drew]
- pci_scan_bars(): check whether bar is 64bits [Drew]
- pci_enable_defaults(): make it return nothing [Drew]
- fix strnang blanks in lib/x86/intel-iommu.h [Drew]
- pci_setup_msi(): fix correct assert() inside [Drew]
- squash patches: 03+17 08+09 10+11 12+14 [Drew]
- remove all "()" for raw defines (numbers) in macros [Drew]

v2:
- move cpu_relax patch to the beginning, and use them in all places [Drew]
- replace all corresponding 256 into PCI_DEVFN_MAX, as well for PCI_BAR_NUM [Drew]
- adding is_power_of_2() to replace ONE_BIT_ONLY() [Drew]
- add SZ_64K macro [Drew]
- declare pci_config_write[wb] in lib/asm-generic/pci-host-bridge.h [Alex]
- edu_reg_read/write() add "l" in func name [Drew]
- drop pci_set_master(), instead, provide pci_cmd_set_clr() [Drew]
- change return code into bool (always) for functions that apply [Drew]
- keep old pci_find_dev() interface [Drew/Alex]
- use __raw_{read|write}*() for both vt-d and edu register read/writes [Alex]
- remove pci_ prefix for all pci_dev fields [Drew]
- replace 0xff in cap_handlers[0xff] into (PCI_CAP_ID_MAX + 1) [Drew]
- make x86/unittest.cfg simpler by using q35 directly with eim=off [Drew]

RFC -> v1:
- when init edu device fail, report_skip() rather than return error [Radim]
- use asserts rather than "static bool inited" to avoid multiple init of components (affects patch 1/2) [Drew]
- moving the first two patches out of the series [Drew]
- int vtd_init(), do not setup_idt() since smp_init() did it [Drew]
- when edu do not have MSI enabled, skip interrupt test [Radim]
- rename vtd_reg_*() into vtd_{read|write}[lq](), and move them to header file [Drew]
- use PAGE_MASK when able [Drew]
- use "&" instead of "|" in intel-iommu init test (three places) [Drew]
- use "vtd_init()" in unit test [Drew]
- mention that where intel-iommu.h comes from [Drew]
- re-written vtd_gcmd_or(), make it also work on even hardware [Drew]
- remove most of the oneline wrapper for VT-d registers, instead, use vtd_{read|write}* with register names [Drew]
- remove useless BDF helpers [Drew]
- move edu device macros into header file [Drew]
- make edu_check_alive static inline [Drew]
- remove all useless wrappers in pci-edu.c [Drew]
- remove pci_dma_dir_t and all its users, instead, use "bool from_device" [Drew]
- not use typedef for structs, to follow Linux/kvm-unit-tests coding style [Drew]
- let pci_dev_init() clean and simple, then provide pci_enable_defaults() for more complicated things [Drew]
- add one more patch to add intel-iommu test into x86/unittest [Radim]
- use 0x60 intr request instead of factorial to trigger edu device interrupt [Drew]
- ...and some other changes I just forgot to note down...

Currently only a very small test scope is covered:

* VT-d init
* DMAR: 4 bytes copy
* IR: MSI

However this series could be a base point to add more test cases for VT-d. The problem is, there are many IOMMU error conditions which are very hard to be triggered in a real guest (IOMMU has merely no interface for guest user, and it's totally running in the background). This piece of work can be a start point if we want to do more complicated things and play around with Intel IOMMU devices (also for IOMMU regression tests).

Please review. Thanks,

Peter Xu (14):
pci: fix missing extern for pci_testdev() x86/asm: add cpu_relax()
libcflat: introduce is_power_of_2()
x86: intel-iommu: add vt-d init test
libcflat: add IS_ALIGNED() macro, and page sizes
libcflat: moving MIN/MAX here
vm/page: provide PGDIR_OFFSET() macro
pci: introduce struct pci_dev
pci: provide pci_scan_bars()
pci: provide pci_enable_defaults()
pci: edu: introduce pci-edu helpers
x86: intel-iommu: add dmar test
pci: add msi support for 32/64bit address
x86: intel-iommu: add IR MSI test

 lib/alloc.c            |   3 -
 lib/libcflat.h         |  14 +++
 lib/pci-edu.c          |  73 ++++++++++++
 lib/pci-edu.h          |  83 +++++++++++++
 lib/pci-host-generic.c |  13 +-
 lib/pci-testdev.c      |  10 +-
 lib/pci.c              | 161 +++++++++++++++++++++----
 lib/pci.h              |  42 +++++--
 lib/x86/asm/barrier.h  |  11 ++
 lib/x86/asm/page.h     |   3 +
 lib/x86/intel-iommu.c  | 314 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/intel-iommu.h  | 142 ++++++++++++++++++++++
 lib/x86/vm.c           |   4 +-
 x86/Makefile.common    |   1 +
 x86/Makefile.x86_64    |   2 +
 x86/intel-iommu.c      | 120 +++++++++++++++++++
 x86/unittests.cfg      |   7 ++
 x86/vmexit.c           |  27 ++---
 18 files changed, 969 insertions(+), 61 deletions(-)
 create mode 100644 lib/pci-edu.c
 create mode 100644 lib/pci-edu.h
 create mode 100644 lib/x86/intel-iommu.c
 create mode 100644 lib/x86/intel-iommu.h
 create mode 100644 x86/intel-iommu.c
Signature · 6 lines
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#24 lore raw
In reply to Peter Xu
Quoted 1 line · Peter Xu

Please review. Thanks,

Had to make the changes below to make it compile for aarch64/arm (Did not check the validity though)

lib/pci-host-generic.c +3 -3
index 8bad8b12a466..958ef70a5922 100644
@@ -165,7 +165,7 @@ static struct pci_host_bridge *pci_dt_probe(void)
 	return host;
 }
 
-static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
+static bool pci_alloc_resource(struct pci_dev *dev, int bar_num, u64 *addr)
 {
 	struct pci_host_bridge *host = pci_host_bridge;
 	struct pci_addr_space *as = &host->addr_space[0];
@@ -192,7 +192,7 @@ static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
 
 	if (i >= host->nr_addr_spaces) {
 		printf("%s: warning: can't satisfy request for ", __func__);
-		pci_dev_print_id(dev);
+		pci_dev_print_id(dev->bdf);
 		printf(" ");
 		pci_bar_print(dev, bar_num);
 		printf("\n");
@@ -238,7 +238,7 @@ bool pci_probe(void)
 		for (i = 0; i < 6; i++) {
 			u64 addr;
 
-			if (pci_alloc_resource(dev, i, &addr)) {
+			if (pci_alloc_resource(&pci_dev, i, &addr)) {
 				pci_bar_set_addr(&pci_dev, i, addr);
 
 				if (pci_bar_is_memory(&pci_dev, i))
author
#25 lore raw
Quoted 29 lines · Alexander Gordeev
Quoted 1 line · Peter Xu

Please review. Thanks,

Had to make the changes below to make it compile for aarch64/arm (Did not check the validity though)

lib/pci-host-generic.c +2 -2
index 8bad8b12a466..958ef70a5922 100644
@@ -165,7 +165,7 @@ static struct pci_host_bridge *pci_dt_probe(void)
 	return host;
 }
 
-static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
+static bool pci_alloc_resource(struct pci_dev *dev, int bar_num, u64 *addr)
 {
 	struct pci_host_bridge *host = pci_host_bridge;
 	struct pci_addr_space *as = &host->addr_space[0];
@@ -192,7 +192,7 @@ static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
 
 	if (i >= host->nr_addr_spaces) {
 		printf("%s: warning: can't satisfy request for ", __func__);
-		pci_dev_print_id(dev);
+		pci_dev_print_id(dev->bdf);
 		printf(" ");
 		pci_bar_print(dev, bar_num);
 		printf("\n");

lib/pci-host-generic.c bool pci_probe(void) · line 244

Quoted 8 lines
lib/pci-host-generic.c +1 -1
@@ -238,7 +238,7 @@ bool pci_probe(void)
 		for (i = 0; i < 6; i++) {
 			u64 addr;
 
-			if (pci_alloc_resource(dev, i, &addr)) {
+			if (pci_alloc_resource(&pci_dev, i, &addr)) {
 				pci_bar_set_addr(&pci_dev, i, addr);
 
 				if (pci_bar_is_memory(&pci_dev, i))

Hi, Alex,

Thanks for helping verify this series against ARM. :-) (and sorry if I broke it :( )

However I may have missed the point here - could you please help paste the compile error message if without the above patch? I see that the patch changed interface for pci_alloc_resource(), but why the old one cannot work?

-- peterx

#26 lore raw
Quoted 41 lines · Peter Xu
Quoted 29 lines · Alexander Gordeev
Quoted 1 line · Peter Xu

Please review. Thanks,

Had to make the changes below to make it compile for aarch64/arm (Did not check the validity though)

lib/pci-host-generic.c +2 -2
index 8bad8b12a466..958ef70a5922 100644
@@ -165,7 +165,7 @@ static struct pci_host_bridge *pci_dt_probe(void)
 	return host;
 }
 
-static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
+static bool pci_alloc_resource(struct pci_dev *dev, int bar_num, u64 *addr)
 {
 	struct pci_host_bridge *host = pci_host_bridge;
 	struct pci_addr_space *as = &host->addr_space[0];
@@ -192,7 +192,7 @@ static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
 
 	if (i >= host->nr_addr_spaces) {
 		printf("%s: warning: can't satisfy request for ", __func__);
-		pci_dev_print_id(dev);
+		pci_dev_print_id(dev->bdf);
 		printf(" ");
 		pci_bar_print(dev, bar_num);
 		printf("\n");

lib/pci-host-generic.c bool pci_probe(void) · line 244

Quoted 8 lines
lib/pci-host-generic.c +1 -1
@@ -238,7 +238,7 @@ bool pci_probe(void)
 		for (i = 0; i < 6; i++) {
 			u64 addr;
 
-			if (pci_alloc_resource(dev, i, &addr)) {
+			if (pci_alloc_resource(&pci_dev, i, &addr)) {
 				pci_bar_set_addr(&pci_dev, i, addr);
 
 				if (pci_bar_is_memory(&pci_dev, i))

Hi, Alex,

Quoted 7 lines

Thanks for helping verify this series against ARM. :-) (and sorry if I broke it :( )

However I may have missed the point here - could you please help paste the compile error message if without the above patch? I see that the patch changed interface for pci_alloc_resource(), but why the old one cannot work?

aarch64-linux-gnu-gcc -std=gnu99 -ffreestanding -Wextra -O2 -I lib -I lib/libfdt -g -MMD -MF lib/.pci-host-generic.d -Wall -Werror -fomit-frame-pointer -Wno-frame-address -c -o lib/pci-host-generic.o lib/pci-host-generic.c
lib/pci-host-generic.c: In function ‘pci_alloc_resource’:
lib/pci-host-generic.c:178:2: error: passing argument 1 of ‘pci_bar_size’ makes pointer from integer without a cast [-Werror]
size = pci_bar_size(dev, bar_num);
^
In file included from lib/pci-host-generic.c:11:0:
lib/pci.h:45:20: note: expected ‘struct pci_dev *’ but argument is of type ‘pcidevaddr_t’

 extern phys_addr_t pci_bar_size(struct pci_dev *dev, int bar_num);
                    ^

lib/pci-host-generic.c:182:2: error: passing argument 1 of ‘pci_bar_get’ makes pointer from integer without a cast [-Werror]
bar = pci_bar_get(dev, bar_num);
^
In file included from lib/pci-host-generic.c:11:0:
lib/pci.h:46:17: note: expected ‘struct pci_dev *’ but argument is of type ‘pcidevaddr_t’

 extern uint32_t pci_bar_get(struct pci_dev *dev, int bar_num);
                 ^

lib/pci-host-generic.c:197:3: error: passing argument 1 of ‘pci_bar_print’ makes pointer from integer without a cast [-Werror]
pci_bar_print(dev, bar_num);
^
In file included from lib/pci-host-generic.c:11:0:
lib/pci.h:51:13: note: expected ‘struct pci_dev *’ but argument is of type ‘pcidevaddr_t’

 extern void pci_bar_print(struct pci_dev *dev, int bar_num);
             ^

lib/pci-host-generic.c: At top level:
cc1: error: unrecognized command line option "-Wno-frame-address" [-Werror]
cc1: all warnings being treated as errors
make: *** [lib/pci-host-generic.o] Error 1

Quoted 1 line

-- peterx

author
#27 lore raw
Quoted 73 lines · Alexander Gordeev
Quoted 41 lines · Peter Xu
Quoted 29 lines · Alexander Gordeev
Quoted 1 line · Peter Xu

Please review. Thanks,

Had to make the changes below to make it compile for aarch64/arm (Did not check the validity though)

lib/pci-host-generic.c +2 -2
index 8bad8b12a466..958ef70a5922 100644
@@ -165,7 +165,7 @@ static struct pci_host_bridge *pci_dt_probe(void)
 	return host;
 }
 
-static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
+static bool pci_alloc_resource(struct pci_dev *dev, int bar_num, u64 *addr)
 {
 	struct pci_host_bridge *host = pci_host_bridge;
 	struct pci_addr_space *as = &host->addr_space[0];
@@ -192,7 +192,7 @@ static bool pci_alloc_resource(pcidevaddr_t dev, int bar_num, u64 *addr)
 
 	if (i >= host->nr_addr_spaces) {
 		printf("%s: warning: can't satisfy request for ", __func__);
-		pci_dev_print_id(dev);
+		pci_dev_print_id(dev->bdf);
 		printf(" ");
 		pci_bar_print(dev, bar_num);
 		printf("\n");

lib/pci-host-generic.c bool pci_probe(void) · line 244

Quoted 8 lines
lib/pci-host-generic.c +1 -1
@@ -238,7 +238,7 @@ bool pci_probe(void)
 		for (i = 0; i < 6; i++) {
 			u64 addr;
 
-			if (pci_alloc_resource(dev, i, &addr)) {
+			if (pci_alloc_resource(&pci_dev, i, &addr)) {
 				pci_bar_set_addr(&pci_dev, i, addr);
 
 				if (pci_bar_is_memory(&pci_dev, i))

Hi, Alex,

Quoted 7 lines

Thanks for helping verify this series against ARM. :-) (and sorry if I broke it :( )

However I may have missed the point here - could you please help paste the compile error message if without the above patch? I see that the patch changed interface for pci_alloc_resource(), but why the old one cannot work?

aarch64-linux-gnu-gcc -std=gnu99 -ffreestanding -Wextra -O2 -I lib -I lib/libfdt -g -MMD -MF lib/.pci-host-generic.d -Wall -Werror -fomit-frame-pointer -Wno-frame-address -c -o lib/pci-host-generic.o lib/pci-host-generic.c
lib/pci-host-generic.c: In function ‘pci_alloc_resource’:
lib/pci-host-generic.c:178:2: error: passing argument 1 of ‘pci_bar_size’ makes pointer from integer without a cast [-Werror]
size = pci_bar_size(dev, bar_num);
^
In file included from lib/pci-host-generic.c:11:0:
lib/pci.h:45:20: note: expected ‘struct pci_dev *’ but argument is of type ‘pcidevaddr_t’

 extern phys_addr_t pci_bar_size(struct pci_dev *dev, int bar_num);
                    ^

lib/pci-host-generic.c:182:2: error: passing argument 1 of ‘pci_bar_get’ makes pointer from integer without a cast [-Werror]
bar = pci_bar_get(dev, bar_num);
^
In file included from lib/pci-host-generic.c:11:0:
lib/pci.h:46:17: note: expected ‘struct pci_dev *’ but argument is of type ‘pcidevaddr_t’

 extern uint32_t pci_bar_get(struct pci_dev *dev, int bar_num);
                 ^
lib/pci-host-generic.c:197:3: error: passing argument 1 of ‘pci_bar_print’ makes pointer from integer without a cast [-Werror]
   pci_bar_print(dev, bar_num);
   ^
In file included from lib/pci-host-generic.c:11:0:
lib/pci.h:51:13: note: expected ‘struct pci_dev *’ but argument is of type ‘pcidevaddr_t’
 extern void pci_bar_print(struct pci_dev *dev, int bar_num);
             ^
Quoted 4 lines

lib/pci-host-generic.c: At top level:
cc1: error: unrecognized command line option "-Wno-frame-address" [-Werror]
cc1: all warnings being treated as errors
make: *** [lib/pci-host-generic.o] Error 1

Oops! Will repost with squashing your fix into patch 8/14. Thanks!

-- peterx