[PATCH v10 07/10] OF: Introduce helper function for getting PCI do=

From: Liviu Dudau
Date: Mon Aug 04 2014 - 12:03:04 EST


Add of_pci_get_domain_nr() to retrieve the PCI domain number
of a given device from DT. If the information is not present,
the function can be requested to allocate a new domain number.

Cc: Bjorn Helgaas <bhelgaas@xxxxxxxxxx>
Cc: Arnd Bergmann <arnd@xxxxxxxx>
Cc: Grant Likely <grant.likely@xxxxxxxxxx>
Cc: Rob Herring <robh+dt@xxxxxxxxxx>
Reviewed-by: Catalin Marinas <catalin.marinas@xxxxxxx>
Signed-off-by: Liviu Dudau <Liviu.Dudau@xxxxxxx>
---
drivers/of/of_pci.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++=
++++
include/linux/of_pci.h | 7 ++++++
2 files changed, 69 insertions(+)

diff --git a/drivers/of/of_pci.c b/drivers/of/of_pci.c
index 8481996..3560401 100644
--- a/drivers/of/of_pci.c
+++ b/drivers/of/of_pci.c
@@ -3,6 +3,8 @@
#include <linux/of.h>
#include <linux/of_pci.h>
=20
+#include "of_private.h"
+
static inline int __of_pci_pci_compare(struct device_node *node,
=09=09=09=09 unsigned int data)
{
@@ -89,6 +91,66 @@ int of_pci_parse_bus_range(struct device_node *node, str=
uct resource *res)
}
EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
=20
+static atomic_t of_domain_nr =3D ATOMIC_INIT(-1);
+
+/*
+ * Get the maximum value for a domain number from the device tree
+ */
+static int of_get_max_pci_domain_nr(void)
+{
+=09struct alias_prop *app;
+=09int max_domain =3D -1;
+
+=09mutex_lock(&of_mutex);
+=09list_for_each_entry(app, &aliases_lookup, link) {
+=09=09if (strncmp(app->stem, "pci-domain", 10) !=3D 0)
+=09=09=09continue;
+
+=09=09max_domain =3D max(max_domain, app->id);
+=09}
+=09mutex_unlock(&of_mutex);
+
+=09return max_domain;
+}
+
+/**
+ * This function will try to obtain the host bridge domain number by
+ * using of_alias_get_id() call with "pci-domain" as a stem. If that
+ * fails, a local allocator will be used. The local allocator can
+ * be requested to return a new domain_nr if the information is missing
+ * from the device tree.
+ *
+ * @node: device tree node with the domain information
+ * @allocate_if_missing: if DT lacks information about the domain nr,
+ * allocate a new number.
+ *
+ * Returns the associated domain number from DT, or a new domain number
+ * if DT information is missing and @allocate_if_missing is true. If
+ * @allocate_if_missing is false then the last allocated domain number
+ * will be returned.
+ */
+int of_pci_get_domain_nr(struct device_node *node, bool allocate_if_missin=
g)
+{
+=09int domain;
+
+=09domain =3D atomic_read(&of_domain_nr);
+=09if (of_domain_nr =3D=3D -1) {
+=09=09/* first run, get max defined domain nr in device tree */
+=09=09domain =3D of_get_max_pci_domain_nr();
+=09=09/* then set the start value for allocator to be max + 1 */
+=09=09atomic_set(&of_domain_nr, domain + 1);
+=09}
+=09domain =3D of_alias_get_id(node, "pci-domain");
+=09if (domain =3D=3D -ENODEV) {
+=09=09domain =3D atomic_read(&of_domain_nr);
+=09=09if (allocate_if_missing)
+=09=09=09atomic_inc(&of_domain_nr);
+=09}
+
+=09return domain;
+}
+EXPORT_SYMBOL_GPL(of_pci_get_domain_nr);
+
#ifdef CONFIG_PCI_MSI
=20
static LIST_HEAD(of_pci_msi_chip_list);
diff --git a/include/linux/of_pci.h b/include/linux/of_pci.h
index dde3a4a..3a3824c 100644
--- a/include/linux/of_pci.h
+++ b/include/linux/of_pci.h
@@ -15,6 +15,7 @@ struct device_node *of_pci_find_child_device(struct devic=
e_node *parent,
int of_pci_get_devfn(struct device_node *np);
int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin);
int of_pci_parse_bus_range(struct device_node *node, struct resource *res)=
;
+int of_pci_get_domain_nr(struct device_node *node, bool allocate_if_missin=
g);
#else
static inline int of_irq_parse_pci(const struct pci_dev *pdev, struct of_p=
handle_args *out_irq)
{
@@ -43,6 +44,12 @@ of_pci_parse_bus_range(struct device_node *node, struct =
resource *res)
{
=09return -EINVAL;
}
+
+static inline int
+of_pci_get_domain_nr(struct device_node *node, bool allocate_if_missing)
+{
+=09return -1;
+}
#endif
=20
#if defined(CONFIG_OF) && defined(CONFIG_PCI_MSI)
--=20
2.0.4

--d6Gm4EdcadzBjdND--

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/