Author: ian
Date: Sun Oct 26 01:58:18 2014
New Revision: 273655
URL: https://svnweb.freebsd.org/changeset/base/273655

Log:
  MFC r270953, r270958, r270960, r271190, r271199, r271202:
  
    Create a mechanism for looking up a device_t associated with an ofw/fdt
    xref handle, and for registering that association.  Also use the same data
    for faster translations between node and xref handles.
  
    Add OF_xref_from_device() so that there's no need to have an intermediate
    call to ofw_bus_get_node() to lookup info that's already in the xreflist.
  
    When registering an association between a device and an xref phandle, create
    an entry in the xref list if one doesn't already exist for the given handle.

Modified:
  stable/10/sys/dev/ofw/openfirm.c
  stable/10/sys/dev/ofw/openfirm.h
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/sys/dev/ofw/openfirm.c
==============================================================================
--- stable/10/sys/dev/ofw/openfirm.c    Sun Oct 26 01:47:55 2014        
(r273654)
+++ stable/10/sys/dev/ofw/openfirm.c    Sun Oct 26 01:58:18 2014        
(r273655)
@@ -62,7 +62,10 @@ __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
+#include <sys/lock.h>
 #include <sys/malloc.h>
+#include <sys/mutex.h>
+#include <sys/queue.h>
 #include <sys/systm.h>
 #include <sys/endian.h>
 
@@ -84,6 +87,106 @@ static ofw_t                ofw_obj;
 static struct ofw_kobj ofw_kernel_obj;
 static struct kobj_ops ofw_kernel_kops;
 
+struct xrefinfo {
+       phandle_t       xref;
+       phandle_t       node;
+       device_t        dev;
+       SLIST_ENTRY(xrefinfo) next_entry;
+};
+
+static SLIST_HEAD(, xrefinfo) xreflist = SLIST_HEAD_INITIALIZER(xreflist);
+static struct mtx xreflist_lock;
+static boolean_t xref_init_done;
+
+#define        FIND_BY_XREF    0
+#define        FIND_BY_NODE    1
+#define        FIND_BY_DEV     2
+
+/*
+ * xref-phandle-device lookup helper routines.
+ *
+ * As soon as we are able to use malloc(), walk the node tree and build a list
+ * of info that cross-references node handles, xref handles, and device_t
+ * instances.  This list exists primarily to allow association of a device_t
+ * with an xref handle, but it is also used to speed up translation between 
xref
+ * and node handles.  Before malloc() is available we have to recursively 
search
+ * the node tree each time we want to translate between a node and xref handle.
+ * Afterwards we can do the translations by searching this much shorter list.
+ */
+static void
+xrefinfo_create(phandle_t node)
+{
+       struct xrefinfo * xi;
+       phandle_t child, xref;
+
+       /*
+        * Recursively descend from parent, looking for nodes with a property
+        * named either "phandle", "ibm,phandle", or "linux,phandle".  For each
+        * such node found create an entry in the xreflist.
+        */
+       for (child = OF_child(node); child != 0; child = OF_peer(child)) {
+               xrefinfo_create(child);
+               if (OF_getencprop(child, "phandle", &xref, sizeof(xref)) ==
+                   -1 && OF_getencprop(child, "ibm,phandle", &xref,
+                   sizeof(xref)) == -1 && OF_getencprop(child,
+                   "linux,phandle", &xref, sizeof(xref)) == -1)
+                       continue;
+               xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK | M_ZERO);
+               xi->node = child;
+               xi->xref = xref;
+               SLIST_INSERT_HEAD(&xreflist, xi, next_entry);
+       }
+}
+
+static void
+xrefinfo_init(void *unsed)
+{
+
+       /*
+        * There is no locking during this init because it runs much earlier
+        * than any of the clients/consumers of the xref list data, but we do
+        * initialize the mutex that will be used for access later.
+        */
+       mtx_init(&xreflist_lock, "OF xreflist lock", NULL, MTX_DEF);
+       xrefinfo_create(OF_peer(0));
+       xref_init_done = true;
+}
+SYSINIT(xrefinfo, SI_SUB_KMEM, SI_ORDER_ANY, xrefinfo_init, NULL);
+
+static struct xrefinfo *
+xrefinfo_find(phandle_t phandle, int find_by)
+{
+       struct xrefinfo *rv, *xi;
+
+       rv = NULL;
+       mtx_lock(&xreflist_lock);
+       SLIST_FOREACH(xi, &xreflist, next_entry) {
+               if ((find_by == FIND_BY_XREF && phandle == xi->xref) ||
+                   (find_by == FIND_BY_NODE && phandle == xi->node) ||
+                   (find_by == FIND_BY_DEV && phandle == (uintptr_t)xi->dev)) {
+                       rv = xi;
+                       break;
+               }
+       }
+       mtx_unlock(&xreflist_lock);
+       return (rv);
+}
+
+static struct xrefinfo *
+xrefinfo_add(phandle_t node, phandle_t xref, device_t dev)
+{
+       struct xrefinfo *xi;
+
+       xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK);
+       xi->node = node;
+       xi->xref = xref;
+       xi->dev  = dev;
+       mtx_lock(&xreflist_lock);
+       SLIST_INSERT_HEAD(&xreflist, xi, next_entry);
+       mtx_unlock(&xreflist_lock);
+       return (xi);
+}
+
 /*
  * OFW install routines.  Highest priority wins, equal priority also
  * overrides allowing last-set to win.
@@ -465,29 +568,87 @@ OF_child_xref_phandle(phandle_t parent, 
 phandle_t
 OF_node_from_xref(phandle_t xref)
 {
+       struct xrefinfo *xi;
        phandle_t node;
 
-       node = OF_child_xref_phandle(OF_peer(0), xref);
-       if (node == -1)
-               return (xref);
+       if (xref_init_done) {
+               if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
+                       return (xref);
+               return (xi->node);
+       }
 
+       if ((node = OF_child_xref_phandle(OF_peer(0), xref)) == -1)
+               return (xref);
        return (node);
 }
 
 phandle_t
 OF_xref_from_node(phandle_t node)
 {
+       struct xrefinfo *xi;
        phandle_t xref;
 
+       if (xref_init_done) {
+               if ((xi = xrefinfo_find(node, FIND_BY_NODE)) == NULL)
+                       return (node);
+               return (xi->xref);
+       }
+
        if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) ==
            -1 && OF_getencprop(node, "ibm,phandle", &xref,
            sizeof(xref)) == -1 && OF_getencprop(node,
            "linux,phandle", &xref, sizeof(xref)) == -1)
                return (node);
-
        return (xref);
 }
 
+device_t
+OF_device_from_xref(phandle_t xref)
+{
+       struct xrefinfo *xi;
+
+       if (xref_init_done) {
+               if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
+                       return (NULL);
+               return (xi->dev);
+       }
+       panic("Attempt to find device before xreflist_init");
+}
+
+phandle_t
+OF_xref_from_device(device_t dev)
+{
+       struct xrefinfo *xi;
+
+       if (xref_init_done) {
+               if ((xi = xrefinfo_find((uintptr_t)dev, FIND_BY_DEV)) == NULL)
+                       return (0);
+               return (xi->xref);
+       }
+       panic("Attempt to find xref before xreflist_init");
+}
+
+int
+OF_device_register_xref(phandle_t xref, device_t dev)
+{
+       struct xrefinfo *xi;
+
+       /*
+        * If the given xref handle doesn't already exist in the list then we
+        * add a list entry.  In theory this can only happen on a system where
+        * nodes don't contain phandle properties and xref and node handles are
+        * synonymous, so the xref handle is added as the node handle as well.
+        */
+       if (xref_init_done) {
+               if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
+                       xrefinfo_add(xref, xref, dev);
+               else 
+                       xi->dev = dev;
+               return (0);
+       }
+       panic("Attempt to register device before xreflist_init");
+}
+
 /*  Call the method in the scope of a given instance. */
 int
 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,

Modified: stable/10/sys/dev/ofw/openfirm.h
==============================================================================
--- stable/10/sys/dev/ofw/openfirm.h    Sun Oct 26 01:47:55 2014        
(r273654)
+++ stable/10/sys/dev/ofw/openfirm.h    Sun Oct 26 01:58:18 2014        
(r273655)
@@ -133,6 +133,17 @@ ssize_t            OF_package_to_path(phandle_t no
 phandle_t      OF_node_from_xref(phandle_t xref);
 phandle_t      OF_xref_from_node(phandle_t node);
 
+/*
+ * When properties contain references to other nodes using xref handles it is
+ * often necessary to use interfaces provided by the driver for the referenced
+ * instance.  These routines allow a driver that provides such an interface to
+ * register its association with an xref handle, and for other drivers to 
obtain
+ * the device_t associated with an xref handle.
+ */
+device_t       OF_device_from_xref(phandle_t xref);
+phandle_t      OF_xref_from_device(device_t dev);
+int            OF_device_register_xref(phandle_t xref, device_t dev);
+
 /* Device I/O functions */
 ihandle_t      OF_open(const char *path);
 void           OF_close(ihandle_t instance);
_______________________________________________
[email protected] mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "[email protected]"

Reply via email to