Merge tag 'xfs-5.13-fixes-1' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-2.6-microblaze.git] / kernel / resource.c
index 7e00239..ca9f519 100644 (file)
@@ -502,6 +502,34 @@ int __weak page_is_ram(unsigned long pfn)
 }
 EXPORT_SYMBOL_GPL(page_is_ram);
 
+static int __region_intersects(resource_size_t start, size_t size,
+                       unsigned long flags, unsigned long desc)
+{
+       struct resource res;
+       int type = 0; int other = 0;
+       struct resource *p;
+
+       res.start = start;
+       res.end = start + size - 1;
+
+       for (p = iomem_resource.child; p ; p = p->sibling) {
+               bool is_type = (((p->flags & flags) == flags) &&
+                               ((desc == IORES_DESC_NONE) ||
+                                (desc == p->desc)));
+
+               if (resource_overlaps(p, &res))
+                       is_type ? type++ : other++;
+       }
+
+       if (type == 0)
+               return REGION_DISJOINT;
+
+       if (other == 0)
+               return REGION_INTERSECTS;
+
+       return REGION_MIXED;
+}
+
 /**
  * region_intersects() - determine intersection of region with known resources
  * @start: region start address
@@ -525,31 +553,13 @@ EXPORT_SYMBOL_GPL(page_is_ram);
 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
                      unsigned long desc)
 {
-       struct resource res;
-       int type = 0; int other = 0;
-       struct resource *p;
-
-       res.start = start;
-       res.end = start + size - 1;
+       int ret;
 
        read_lock(&resource_lock);
-       for (p = iomem_resource.child; p ; p = p->sibling) {
-               bool is_type = (((p->flags & flags) == flags) &&
-                               ((desc == IORES_DESC_NONE) ||
-                                (desc == p->desc)));
-
-               if (resource_overlaps(p, &res))
-                       is_type ? type++ : other++;
-       }
+       ret = __region_intersects(start, size, flags, desc);
        read_unlock(&resource_lock);
 
-       if (type == 0)
-               return REGION_DISJOINT;
-
-       if (other == 0)
-               return REGION_INTERSECTS;
-
-       return REGION_MIXED;
+       return ret;
 }
 EXPORT_SYMBOL_GPL(region_intersects);
 
@@ -1150,31 +1160,16 @@ struct address_space *iomem_get_mapping(void)
        return smp_load_acquire(&iomem_inode)->i_mapping;
 }
 
-/**
- * __request_region - create a new busy resource region
- * @parent: parent resource descriptor
- * @start: resource start address
- * @n: resource region size
- * @name: reserving caller's ID string
- * @flags: IO resource flags
- */
-struct resource * __request_region(struct resource *parent,
+static int __request_region_locked(struct resource *res, struct resource *parent,
                                   resource_size_t start, resource_size_t n,
                                   const char *name, int flags)
 {
        DECLARE_WAITQUEUE(wait, current);
-       struct resource *res = alloc_resource(GFP_KERNEL);
-       struct resource *orig_parent = parent;
-
-       if (!res)
-               return NULL;
 
        res->name = name;
        res->start = start;
        res->end = start + n - 1;
 
-       write_lock(&resource_lock);
-
        for (;;) {
                struct resource *conflict;
 
@@ -1210,13 +1205,40 @@ struct resource * __request_region(struct resource *parent,
                        continue;
                }
                /* Uhhuh, that didn't work out.. */
-               free_resource(res);
-               res = NULL;
-               break;
+               return -EBUSY;
        }
+
+       return 0;
+}
+
+/**
+ * __request_region - create a new busy resource region
+ * @parent: parent resource descriptor
+ * @start: resource start address
+ * @n: resource region size
+ * @name: reserving caller's ID string
+ * @flags: IO resource flags
+ */
+struct resource *__request_region(struct resource *parent,
+                                 resource_size_t start, resource_size_t n,
+                                 const char *name, int flags)
+{
+       struct resource *res = alloc_resource(GFP_KERNEL);
+       int ret;
+
+       if (!res)
+               return NULL;
+
+       write_lock(&resource_lock);
+       ret = __request_region_locked(res, parent, start, n, name, flags);
        write_unlock(&resource_lock);
 
-       if (res && orig_parent == &iomem_resource)
+       if (ret) {
+               free_resource(res);
+               return NULL;
+       }
+
+       if (parent == &iomem_resource)
                revoke_iomem(res);
 
        return res;
@@ -1758,25 +1780,56 @@ static struct resource *__request_free_mem_region(struct device *dev,
 {
        resource_size_t end, addr;
        struct resource *res;
+       struct region_devres *dr = NULL;
 
        size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
        end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
        addr = end - size + 1UL;
 
+       res = alloc_resource(GFP_KERNEL);
+       if (!res)
+               return ERR_PTR(-ENOMEM);
+
+       if (dev) {
+               dr = devres_alloc(devm_region_release,
+                               sizeof(struct region_devres), GFP_KERNEL);
+               if (!dr) {
+                       free_resource(res);
+                       return ERR_PTR(-ENOMEM);
+               }
+       }
+
+       write_lock(&resource_lock);
        for (; addr > size && addr >= base->start; addr -= size) {
-               if (region_intersects(addr, size, 0, IORES_DESC_NONE) !=
+               if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
                                REGION_DISJOINT)
                        continue;
 
-               if (dev)
-                       res = devm_request_mem_region(dev, addr, size, name);
-               else
-                       res = request_mem_region(addr, size, name);
-               if (!res)
-                       return ERR_PTR(-ENOMEM);
+               if (__request_region_locked(res, &iomem_resource, addr, size,
+                                               name, 0))
+                       break;
+
+               if (dev) {
+                       dr->parent = &iomem_resource;
+                       dr->start = addr;
+                       dr->n = size;
+                       devres_add(dev, dr);
+               }
+
                res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
+               write_unlock(&resource_lock);
+
+               /*
+                * A driver is claiming this region so revoke any mappings.
+                */
+               revoke_iomem(res);
                return res;
        }
+       write_unlock(&resource_lock);
+
+       free_resource(res);
+       if (dr)
+               devres_free(dr);
 
        return ERR_PTR(-ERANGE);
 }