All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ahmad Fatoum <ahmad@a3f.at>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <ahmad@a3f.at>
Subject: [PATCH 7/9] PCI: port Linux pci_find_capability
Date: Sun, 28 Feb 2021 20:08:34 +0100	[thread overview]
Message-ID: <20210228190836.1451663-7-ahmad@a3f.at> (raw)
In-Reply-To: <20210228190836.1451663-1-ahmad@a3f.at>

Incomding Linux virtio_pci code uses these functions, so port them over.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 drivers/pci/pci.c   | 90 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/pci.h |  5 +++
 2 files changed, 95 insertions(+)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 7d1024d8d1d2..945a983387ed 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -506,6 +506,96 @@ int pci_enable_device(struct pci_dev *dev)
 }
 EXPORT_SYMBOL(pci_enable_device);
 
+static u8 __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
+				  u8 pos, int cap, int *ttl)
+{
+	u8 id;
+	u16 ent;
+
+	pci_bus_read_config_byte(bus, devfn, pos, &pos);
+
+	while ((*ttl)--) {
+		if (pos < 0x40)
+			break;
+		pos &= ~3;
+		pci_bus_read_config_word(bus, devfn, pos, &ent);
+
+		id = ent & 0xff;
+		if (id == 0xff)
+			break;
+		if (id == cap)
+			return pos;
+		pos = (ent >> 8);
+	}
+	return 0;
+}
+
+static u8 __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
+			      u8 pos, int cap)
+{
+	int ttl = PCI_FIND_CAP_TTL;
+
+	return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
+}
+
+u8 pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
+{
+	return __pci_find_next_cap(dev->bus, dev->devfn,
+				   pos + PCI_CAP_LIST_NEXT, cap);
+}
+EXPORT_SYMBOL_GPL(pci_find_next_capability);
+
+static u8 __pci_bus_find_cap_start(struct pci_bus *bus,
+				    unsigned int devfn, u8 hdr_type)
+{
+	u16 status;
+
+	pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
+	if (!(status & PCI_STATUS_CAP_LIST))
+		return 0;
+
+	switch (hdr_type) {
+	case PCI_HEADER_TYPE_NORMAL:
+	case PCI_HEADER_TYPE_BRIDGE:
+		return PCI_CAPABILITY_LIST;
+	case PCI_HEADER_TYPE_CARDBUS:
+		return PCI_CB_CAPABILITY_LIST;
+	}
+
+	return 0;
+}
+
+/**
+ * pci_find_capability - query for devices' capabilities
+ * @dev: PCI device to query
+ * @cap: capability code
+ *
+ * Tell if a device supports a given PCI capability.
+ * Returns the address of the requested capability structure within the
+ * device's PCI configuration space or 0 in case the device does not
+ * support it.  Possible values for @cap include:
+ *
+ *  %PCI_CAP_ID_PM           Power Management
+ *  %PCI_CAP_ID_AGP          Accelerated Graphics Port
+ *  %PCI_CAP_ID_VPD          Vital Product Data
+ *  %PCI_CAP_ID_SLOTID       Slot Identification
+ *  %PCI_CAP_ID_MSI          Message Signalled Interrupts
+ *  %PCI_CAP_ID_CHSWP        CompactPCI HotSwap
+ *  %PCI_CAP_ID_PCIX         PCI-X
+ *  %PCI_CAP_ID_EXP          PCI Express
+ */
+u8 pci_find_capability(struct pci_dev *dev, int cap)
+{
+	u8 pos;
+
+	pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
+	if (pos)
+		pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
+
+	return pos;
+}
+EXPORT_SYMBOL(pci_find_capability);
+
 static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f,
 			  struct pci_fixup *end)
 {
diff --git a/include/linux/pci.h b/include/linux/pci.h
index c742570e3684..0c8fed7c8e0d 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -31,6 +31,8 @@
 
 #define PCI_ANY_ID (~0)
 
+#define PCI_FIND_CAP_TTL	48
+
 /*
  * The PCI interface treats multi-function devices as independent
  * devices.  The slot/function address of each device is encoded
@@ -320,6 +322,9 @@ void pci_set_master(struct pci_dev *dev);
 void pci_clear_master(struct pci_dev *dev);
 int pci_enable_device(struct pci_dev *dev);
 
+u8 pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap);
+u8 pci_find_capability(struct pci_dev *dev, int cap);
+
 extern void __iomem *pci_iomap(struct pci_dev *dev, int bar);
 
 /*
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2021-02-28 19:10 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-28 19:08 [PATCH 1/9] virtio: align virtio_config_ops::generation return type with Linux Ahmad Fatoum
2021-02-28 19:08 ` [PATCH 2/9] virtio: remove unused, left-over, virtio_config_ops::set_features Ahmad Fatoum
2021-02-28 19:08 ` [PATCH 3/9] hw_random: virtio: simplify code Ahmad Fatoum
2021-02-28 19:08 ` [PATCH 4/9] dma: move dma_map/unmap_single from ARM to common code Ahmad Fatoum
2021-03-03 12:08   ` Sascha Hauer
2021-03-03 16:12   ` [PATCH] " Ahmad Fatoum
2021-02-28 19:08 ` [PATCH 5/9] virtio: ring: fix erroneous behavior around caches and MMU Ahmad Fatoum
2021-02-28 19:08 ` [PATCH 6/9] virtio: fix support for big-endian clients Ahmad Fatoum
2021-02-28 19:08 ` Ahmad Fatoum [this message]
2021-02-28 19:08 ` [PATCH 8/9] virtio: support virtio-based device drivers over PCI Ahmad Fatoum
2021-02-28 19:08 ` [PATCH 9/9] virtio: virtio-pci: restrict MIPS support to MMU configuration Ahmad Fatoum
2021-03-02 14:28   ` Antony Pavlov
2021-03-03 17:10     ` Ahmad Fatoum
2021-03-01 16:29 ` [PATCH 1/9] virtio: align virtio_config_ops::generation return type with Linux Sascha Hauer

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=20210228190836.1451663-7-ahmad@a3f.at \
    --to=ahmad@a3f.at \
    --cc=barebox@lists.infradead.org \
    /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.