Merge remote-tracking branch 'remotes/powerpc/topic/ppc-kvm' into kvm-ppc-next
[linux-2.6-microblaze.git] / kernel / resource.c
index d22423e..7ea4306 100644 (file)
@@ -326,7 +326,7 @@ EXPORT_SYMBOL(release_resource);
  *
  * If a resource is found, returns 0 and @*res is overwritten with the part
  * of the resource that's within [@start..@end]; if none is found, returns
- * -1 or -EINVAL for other invalid parameters.
+ * -ENODEV.  Returns -EINVAL for invalid parameters.
  *
  * This function walks the whole tree and not just first level children
  * unless @first_lvl is true.
@@ -342,6 +342,7 @@ static int find_next_iomem_res(resource_size_t start, resource_size_t end,
                               unsigned long flags, unsigned long desc,
                               bool first_lvl, struct resource *res)
 {
+       bool siblings_only = true;
        struct resource *p;
 
        if (!res)
@@ -352,29 +353,43 @@ static int find_next_iomem_res(resource_size_t start, resource_size_t end,
 
        read_lock(&resource_lock);
 
-       for (p = iomem_resource.child; p; p = next_resource(p, first_lvl)) {
-               if ((p->flags & flags) != flags)
-                       continue;
-               if ((desc != IORES_DESC_NONE) && (desc != p->desc))
-                       continue;
+       for (p = iomem_resource.child; p; p = next_resource(p, siblings_only)) {
+               /* If we passed the resource we are looking for, stop */
                if (p->start > end) {
                        p = NULL;
                        break;
                }
-               if ((p->end >= start) && (p->start <= end))
-                       break;
+
+               /* Skip until we find a range that matches what we look for */
+               if (p->end < start)
+                       continue;
+
+               /*
+                * Now that we found a range that matches what we look for,
+                * check the flags and the descriptor. If we were not asked to
+                * use only the first level, start looking at children as well.
+                */
+               siblings_only = first_lvl;
+
+               if ((p->flags & flags) != flags)
+                       continue;
+               if ((desc != IORES_DESC_NONE) && (desc != p->desc))
+                       continue;
+
+               /* Found a match, break */
+               break;
+       }
+
+       if (p) {
+               /* copy data */
+               res->start = max(start, p->start);
+               res->end = min(end, p->end);
+               res->flags = p->flags;
+               res->desc = p->desc;
        }
 
        read_unlock(&resource_lock);
-       if (!p)
-               return -1;
-
-       /* copy data */
-       res->start = max(start, p->start);
-       res->end = min(end, p->end);
-       res->flags = p->flags;
-       res->desc = p->desc;
-       return 0;
+       return p ? 0 : -ENODEV;
 }
 
 static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,