All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Gordeev <agordeev@redhat.com>
To: kvm@vger.kernel.org
Cc: Alexander Gordeev <agordeev@redhat.com>,
	Thomas Huth <thuth@redhat.com>, Andrew Jones <drjones@redhat.com>
Subject: [kvm-unit-tests PATCH v7 06/13] pci: Rework pci_bar_addr()
Date: Wed, 17 Aug 2016 14:07:07 +0200	[thread overview]
Message-ID: <38dc0bd6c1350568f4978dba3373249753944a61.1471434672.git.agordeev@redhat.com> (raw)
In-Reply-To: <cover.1471434672.git.agordeev@redhat.com>

This update makes pci_bar_addr() interface 64 bit BARs aware and
introduces a concept of PCI address translation.

An architecutre should implement pci_translate_addr() interface
in order to provide mapping between PCI bus address and CPU
physical address.

Cc: Thomas Huth <thuth@redhat.com>
Cc: Andrew Jones <drjones@redhat.com>
Reviewed-by: Andrew Jones <drjones@redhat.com>
Signed-off-by: Alexander Gordeev <agordeev@redhat.com>
---
 lib/pci.c         | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 lib/pci.h         | 17 ++++++++++++-
 lib/x86/asm/pci.h |  6 +++++
 3 files changed, 90 insertions(+), 6 deletions(-)

diff --git a/lib/pci.c b/lib/pci.c
index ce481bbfadb6..e69e828523d4 100644
--- a/lib/pci.c
+++ b/lib/pci.c
@@ -21,19 +21,71 @@ pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id)
 	return PCIDEVADDR_INVALID;
 }
 
+static uint32_t pci_bar_mask(uint32_t bar)
+{
+	return (bar & PCI_BASE_ADDRESS_SPACE_IO) ?
+		PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK;
+}
+
 static uint32_t pci_bar_get(pcidevaddr_t dev, int bar_num)
 {
 	return pci_config_readl(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
 }
 
-unsigned long pci_bar_addr(pcidevaddr_t dev, int bar_num)
+phys_addr_t pci_bar_addr(pcidevaddr_t dev, int bar_num)
 {
 	uint32_t bar = pci_bar_get(dev, bar_num);
+	uint32_t mask = pci_bar_mask(bar);
+	uint64_t addr = bar & mask;
 
-	if (bar & PCI_BASE_ADDRESS_SPACE_IO)
-		return bar & PCI_BASE_ADDRESS_IO_MASK;
-	else
-		return bar & PCI_BASE_ADDRESS_MEM_MASK;
+	if (pci_bar_is64(dev, bar_num))
+		addr |= (uint64_t)pci_bar_get(dev, bar_num + 1) << 32;
+
+	return pci_translate_addr(dev, addr);
+}
+
+/*
+ * To determine the amount of address space needed by a PCI device,
+ * one must save the original value of the BAR, write a value of
+ * all 1's to the register, and then read it back. The amount of
+ * memory can be then determined by masking the information bits,
+ * performing a bitwise NOT, and incrementing the value by 1.
+ *
+ * The following pci_bar_size_helper() and pci_bar_size() functions
+ * implement the algorithm.
+ */
+static uint32_t pci_bar_size_helper(pcidevaddr_t dev, int bar_num)
+{
+	int off = PCI_BASE_ADDRESS_0 + bar_num * 4;
+	uint32_t bar, size;
+
+	bar = pci_config_readl(dev, off);
+	pci_config_writel(dev, off, ~0u);
+	size = pci_config_readl(dev, off);
+	pci_config_writel(dev, off, bar);
+
+	return size;
+}
+
+phys_addr_t pci_bar_size(pcidevaddr_t dev, int bar_num)
+{
+	uint32_t bar, size;
+
+	size = pci_bar_size_helper(dev, bar_num);
+	if (!size)
+		return 0;
+
+	bar = pci_bar_get(dev, bar_num);
+	size &= pci_bar_mask(bar);
+
+	if (pci_bar_is64(dev, bar_num)) {
+		phys_addr_t size64 = pci_bar_size_helper(dev, bar_num + 1);
+		size64 = (size64 << 32) | size;
+
+		return ~size64 + 1;
+	} else {
+		return ~size + 1;
+	}
 }
 
 bool pci_bar_is_memory(pcidevaddr_t dev, int bar_num)
@@ -47,3 +99,14 @@ bool pci_bar_is_valid(pcidevaddr_t dev, int bar_num)
 {
 	return pci_bar_get(dev, bar_num);
 }
+
+bool pci_bar_is64(pcidevaddr_t dev, int bar_num)
+{
+	uint32_t bar = pci_bar_get(dev, bar_num);
+
+	if (bar & PCI_BASE_ADDRESS_SPACE_IO)
+		return false;
+
+	return (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+		      PCI_BASE_ADDRESS_MEM_TYPE_64;
+}
diff --git a/lib/pci.h b/lib/pci.h
index 066fac77b237..8eec236cb123 100644
--- a/lib/pci.h
+++ b/lib/pci.h
@@ -16,7 +16,22 @@ enum {
 };
 
 extern pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id);
-extern unsigned long pci_bar_addr(pcidevaddr_t dev, int bar_num);
+
+/*
+ * @bar_num in all BAR access functions below is the index of the 32-bit
+ * register starting from the PCI_BASE_ADDRESS_0 offset.
+ *
+ * In cases where the BAR size is 64-bit, a caller should still provide
+ * @bar_num in terms of 32-bit words. For example, if a device has a 64-bit
+ * BAR#0 and a 32-bit BAR#1, then caller should provide 2 to address BAR#1,
+ * not 1.
+ *
+ * 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_addr(pcidevaddr_t dev, int bar_num);
+extern phys_addr_t pci_bar_size(pcidevaddr_t dev, int bar_num);
+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);
 
diff --git a/lib/x86/asm/pci.h b/lib/x86/asm/pci.h
index 821a2c1e180a..7384b91adba1 100644
--- a/lib/x86/asm/pci.h
+++ b/lib/x86/asm/pci.h
@@ -35,4 +35,10 @@ static inline void pci_config_writel(pcidevaddr_t dev, uint8_t reg, uint32_t val
     outl(val, 0xCFC);
 }
 
+static inline
+phys_addr_t pci_translate_addr(pcidevaddr_t dev __unused, uint64_t addr)
+{
+    return addr;
+}
+
 #endif
-- 
1.8.3.1


  parent reply	other threads:[~2016-08-17 12:07 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-17 12:07 [kvm-unit-tests PATCH v7 00/13] PCI bus support Alexander Gordeev
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 01/13] pci: Fix coding style in generic PCI files Alexander Gordeev
2016-08-18 11:37   ` Thomas Huth
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 02/13] pci: x86: Rename pci_config_read() to pci_config_readl() Alexander Gordeev
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 03/13] pci: Add 'extern' to public function declarations Alexander Gordeev
2016-08-17 13:49   ` Andrew Jones
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 04/13] pci: x86: Add remaining PCI configuration space accessors Alexander Gordeev
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 05/13] pci: Factor out pci_bar_get() Alexander Gordeev
2016-08-18 11:39   ` Thomas Huth
2016-08-17 12:07 ` Alexander Gordeev [this message]
2016-09-23  7:14   ` [kvm-unit-tests PATCH v7 06/13] pci: Rework pci_bar_addr() Peter Xu
2016-09-23  8:51     ` Andrew Jones
2016-09-23  8:58       ` Peter Xu
2016-10-12 14:37     ` Alexander Gordeev
2016-10-13  6:40       ` Peter Xu
2016-10-13 14:16         ` Alexander Gordeev
2016-10-14  6:23           ` Peter Xu
2016-10-14  6:55             ` Andrew Jones
2016-10-14  9:09               ` Peter Xu
2016-10-14 12:37             ` Alexander Gordeev
2016-10-19  3:46               ` Peter Xu
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 07/13] pci: Add pci_bar_set_addr() Alexander Gordeev
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 08/13] pci: Add pci_dev_exists() Alexander Gordeev
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 09/13] pci: Add pci_print() Alexander Gordeev
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 10/13] pci: Add generic ECAM host support Alexander Gordeev
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 11/13] arm/arm64: pci: Add PCI bus operation test Alexander Gordeev
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 12/13] pci: Add pci-testdev PCI bus test device Alexander Gordeev
2016-08-17 14:03   ` Andrew Jones
2016-09-23  7:25   ` Peter Xu
2016-09-23  8:55     ` Andrew Jones
2016-10-12 16:54     ` Alexander Gordeev
2016-10-13  6:52       ` Peter Xu
2016-10-13 13:16         ` Alexander Gordeev
2016-10-14  5:01           ` Peter Xu
2016-10-14  7:07             ` Andrew Jones
2016-10-14  9:14               ` Peter Xu
2016-08-17 12:07 ` [kvm-unit-tests PATCH v7 13/13] arm/arm64: pci: Add pci-testdev PCI device operation test Alexander Gordeev
2016-08-17 14:26 ` [kvm-unit-tests PATCH v7 00/13] PCI bus support Andrew Jones
2016-08-23 18:28   ` Alexander Gordeev
2016-09-22 11:10     ` Andrew Jones
2016-09-28  6:33       ` Peter Xu
2016-10-12  8:00         ` Alexander Gordeev
2016-10-12 10:59           ` Peter Xu
2016-10-12 14:35       ` Alexander Gordeev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=38dc0bd6c1350568f4978dba3373249753944a61.1471434672.git.agordeev@redhat.com \
    --to=agordeev@redhat.com \
    --cc=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.