ARM: tegra: dvfs: Add GPU scaling trip-points interfaces
[linux-3.10.git] / kernel / resource.c
index c8dc249..d738698 100644 (file)
@@ -7,7 +7,9 @@
  * Arbitrary resource management.
  */
 
-#include <linux/module.h>
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/export.h>
 #include <linux/errno.h>
 #include <linux/ioport.h>
 #include <linux/init.h>
@@ -19,6 +21,7 @@
 #include <linux/seq_file.h>
 #include <linux/device.h>
 #include <linux/pfn.h>
+#include <linux/mm.h>
 #include <asm/io.h>
 
 
@@ -48,6 +51,14 @@ struct resource_constraint {
 
 static DEFINE_RWLOCK(resource_lock);
 
+/*
+ * For memory hotplug, there is no way to free resource entries allocated
+ * by boot mem after the system is up. So for reusing the resource entry
+ * we need to remember the resource.
+ */
+static struct resource *bootmem_resource_free;
+static DEFINE_SPINLOCK(bootmem_resource_lock);
+
 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
 {
        struct resource *p = v;
@@ -149,6 +160,40 @@ __initcall(ioresources_init);
 
 #endif /* CONFIG_PROC_FS */
 
+static void free_resource(struct resource *res)
+{
+       if (!res)
+               return;
+
+       if (!PageSlab(virt_to_head_page(res))) {
+               spin_lock(&bootmem_resource_lock);
+               res->sibling = bootmem_resource_free;
+               bootmem_resource_free = res;
+               spin_unlock(&bootmem_resource_lock);
+       } else {
+               kfree(res);
+       }
+}
+
+static struct resource *alloc_resource(gfp_t flags)
+{
+       struct resource *res = NULL;
+
+       spin_lock(&bootmem_resource_lock);
+       if (bootmem_resource_free) {
+               res = bootmem_resource_free;
+               bootmem_resource_free = res->sibling;
+       }
+       spin_unlock(&bootmem_resource_lock);
+
+       if (res)
+               memset(res, 0, sizeof(struct resource));
+       else
+               res = kzalloc(sizeof(struct resource), flags);
+
+       return res;
+}
+
 /* Return the conflict entry if you can't request it */
 static struct resource * __request_resource(struct resource *root, struct resource *new)
 {
@@ -515,8 +560,8 @@ out:
  * @root: root resource descriptor
  * @new: resource descriptor desired by caller
  * @size: requested resource region size
- * @min: minimum size to allocate
- * @max: maximum size to allocate
+ * @min: minimum boundary to allocate
+ * @max: maximum boundary to allocate
  * @align: alignment requested, in bytes
  * @alignf: alignment function, optional, called if not NULL
  * @alignf_data: arbitrary data to pass to the @alignf function
@@ -704,32 +749,19 @@ void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
        write_unlock(&resource_lock);
 }
 
-/**
- * adjust_resource - modify a resource's start and size
- * @res: resource to modify
- * @start: new start value
- * @size: new size
- *
- * Given an existing resource, change its start and size to match the
- * arguments.  Returns 0 on success, -EBUSY if it can't fit.
- * Existing children of the resource are assumed to be immutable.
- */
-int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
+static int __adjust_resource(struct resource *res, resource_size_t start,
+                               resource_size_t size)
 {
        struct resource *tmp, *parent = res->parent;
        resource_size_t end = start + size - 1;
        int result = -EBUSY;
 
-       write_lock(&resource_lock);
+       if (!parent)
+               goto skip;
 
        if ((start < parent->start) || (end > parent->end))
                goto out;
 
-       for (tmp = res->child; tmp; tmp = tmp->sibling) {
-               if ((tmp->start < start) || (tmp->end > end))
-                       goto out;
-       }
-
        if (res->sibling && (res->sibling->start <= end))
                goto out;
 
@@ -741,14 +773,40 @@ int adjust_resource(struct resource *res, resource_size_t start, resource_size_t
                        goto out;
        }
 
+skip:
+       for (tmp = res->child; tmp; tmp = tmp->sibling)
+               if ((tmp->start < start) || (tmp->end > end))
+                       goto out;
+
        res->start = start;
        res->end = end;
        result = 0;
 
  out:
+       return result;
+}
+
+/**
+ * adjust_resource - modify a resource's start and size
+ * @res: resource to modify
+ * @start: new start value
+ * @size: new size
+ *
+ * Given an existing resource, change its start and size to match the
+ * arguments.  Returns 0 on success, -EBUSY if it can't fit.
+ * Existing children of the resource are assumed to be immutable.
+ */
+int adjust_resource(struct resource *res, resource_size_t start,
+                       resource_size_t size)
+{
+       int result;
+
+       write_lock(&resource_lock);
+       result = __adjust_resource(res, start, size);
        write_unlock(&resource_lock);
        return result;
 }
+EXPORT_SYMBOL(adjust_resource);
 
 static void __init __reserve_region_with_split(struct resource *root,
                resource_size_t start, resource_size_t end,
@@ -756,7 +814,8 @@ static void __init __reserve_region_with_split(struct resource *root,
 {
        struct resource *parent = root;
        struct resource *conflict;
-       struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
+       struct resource *res = alloc_resource(GFP_ATOMIC);
+       struct resource *next_res = NULL;
 
        if (!res)
                return;
@@ -766,34 +825,76 @@ static void __init __reserve_region_with_split(struct resource *root,
        res->end = end;
        res->flags = IORESOURCE_BUSY;
 
-       conflict = __request_resource(parent, res);
-       if (!conflict)
-               return;
+       while (1) {
 
-       /* failed, split and try again */
-       kfree(res);
+               conflict = __request_resource(parent, res);
+               if (!conflict) {
+                       if (!next_res)
+                               break;
+                       res = next_res;
+                       next_res = NULL;
+                       continue;
+               }
 
-       /* conflict covered whole area */
-       if (conflict->start <= start && conflict->end >= end)
-               return;
+               /* conflict covered whole area */
+               if (conflict->start <= res->start &&
+                               conflict->end >= res->end) {
+                       free_resource(res);
+                       WARN_ON(next_res);
+                       break;
+               }
+
+               /* failed, split and try again */
+               if (conflict->start > res->start) {
+                       end = res->end;
+                       res->end = conflict->start - 1;
+                       if (conflict->end < end) {
+                               next_res = alloc_resource(GFP_ATOMIC);
+                               if (!next_res) {
+                                       free_resource(res);
+                                       break;
+                               }
+                               next_res->name = name;
+                               next_res->start = conflict->end + 1;
+                               next_res->end = end;
+                               next_res->flags = IORESOURCE_BUSY;
+                       }
+               } else {
+                       res->start = conflict->end + 1;
+               }
+       }
 
-       if (conflict->start > start)
-               __reserve_region_with_split(root, start, conflict->start-1, name);
-       if (conflict->end < end)
-               __reserve_region_with_split(root, conflict->end+1, end, name);
 }
 
 void __init reserve_region_with_split(struct resource *root,
                resource_size_t start, resource_size_t end,
                const char *name)
 {
+       int abort = 0;
+
        write_lock(&resource_lock);
-       __reserve_region_with_split(root, start, end, name);
+       if (root->start > start || root->end < end) {
+               pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
+                      (unsigned long long)start, (unsigned long long)end,
+                      root);
+               if (start > root->end || end < root->start)
+                       abort = 1;
+               else {
+                       if (end > root->end)
+                               end = root->end;
+                       if (start < root->start)
+                               start = root->start;
+                       pr_err("fixing request to [0x%llx-0x%llx]\n",
+                              (unsigned long long)start,
+                              (unsigned long long)end);
+               }
+               dump_stack();
+       }
+       if (!abort)
+               __reserve_region_with_split(root, start, end, name);
        write_unlock(&resource_lock);
 }
 
-EXPORT_SYMBOL(adjust_resource);
-
 /**
  * resource_alignment - calculate resource's alignment
  * @res: resource pointer
@@ -840,7 +941,7 @@ struct resource * __request_region(struct resource *parent,
                                   const char *name, int flags)
 {
        DECLARE_WAITQUEUE(wait, current);
-       struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
+       struct resource *res = alloc_resource(GFP_KERNEL);
 
        if (!res)
                return NULL;
@@ -874,7 +975,7 @@ struct resource * __request_region(struct resource *parent,
                        continue;
                }
                /* Uhhuh, that didn't work out.. */
-               kfree(res);
+               free_resource(res);
                res = NULL;
                break;
        }
@@ -908,7 +1009,7 @@ int __check_region(struct resource *parent, resource_size_t start,
                return -EBUSY;
 
        release_resource(res);
-       kfree(res);
+       free_resource(res);
        return 0;
 }
 EXPORT_SYMBOL(__check_region);
@@ -948,7 +1049,7 @@ void __release_region(struct resource *parent, resource_size_t start,
                        write_unlock(&resource_lock);
                        if (res->flags & IORESOURCE_MUXED)
                                wake_up(&muxed_resource_wait);
-                       kfree(res);
+                       free_resource(res);
                        return;
                }
                p = &res->sibling;
@@ -962,6 +1063,109 @@ void __release_region(struct resource *parent, resource_size_t start,
 }
 EXPORT_SYMBOL(__release_region);
 
+#ifdef CONFIG_MEMORY_HOTREMOVE
+/**
+ * release_mem_region_adjustable - release a previously reserved memory region
+ * @parent: parent resource descriptor
+ * @start: resource start address
+ * @size: resource region size
+ *
+ * This interface is intended for memory hot-delete.  The requested region
+ * is released from a currently busy memory resource.  The requested region
+ * must either match exactly or fit into a single busy resource entry.  In
+ * the latter case, the remaining resource is adjusted accordingly.
+ * Existing children of the busy memory resource must be immutable in the
+ * request.
+ *
+ * Note:
+ * - Additional release conditions, such as overlapping region, can be
+ *   supported after they are confirmed as valid cases.
+ * - When a busy memory resource gets split into two entries, the code
+ *   assumes that all children remain in the lower address entry for
+ *   simplicity.  Enhance this logic when necessary.
+ */
+int release_mem_region_adjustable(struct resource *parent,
+                       resource_size_t start, resource_size_t size)
+{
+       struct resource **p;
+       struct resource *res;
+       struct resource *new_res;
+       resource_size_t end;
+       int ret = -EINVAL;
+
+       end = start + size - 1;
+       if ((start < parent->start) || (end > parent->end))
+               return ret;
+
+       /* The alloc_resource() result gets checked later */
+       new_res = alloc_resource(GFP_KERNEL);
+
+       p = &parent->child;
+       write_lock(&resource_lock);
+
+       while ((res = *p)) {
+               if (res->start >= end)
+                       break;
+
+               /* look for the next resource if it does not fit into */
+               if (res->start > start || res->end < end) {
+                       p = &res->sibling;
+                       continue;
+               }
+
+               if (!(res->flags & IORESOURCE_MEM))
+                       break;
+
+               if (!(res->flags & IORESOURCE_BUSY)) {
+                       p = &res->child;
+                       continue;
+               }
+
+               /* found the target resource; let's adjust accordingly */
+               if (res->start == start && res->end == end) {
+                       /* free the whole entry */
+                       *p = res->sibling;
+                       free_resource(res);
+                       ret = 0;
+               } else if (res->start == start && res->end != end) {
+                       /* adjust the start */
+                       ret = __adjust_resource(res, end + 1,
+                                               res->end - end);
+               } else if (res->start != start && res->end == end) {
+                       /* adjust the end */
+                       ret = __adjust_resource(res, res->start,
+                                               start - res->start);
+               } else {
+                       /* split into two entries */
+                       if (!new_res) {
+                               ret = -ENOMEM;
+                               break;
+                       }
+                       new_res->name = res->name;
+                       new_res->start = end + 1;
+                       new_res->end = res->end;
+                       new_res->flags = res->flags;
+                       new_res->parent = res->parent;
+                       new_res->sibling = res->sibling;
+                       new_res->child = NULL;
+
+                       ret = __adjust_resource(res, res->start,
+                                               start - res->start);
+                       if (ret)
+                               break;
+                       res->sibling = new_res;
+                       new_res = NULL;
+               }
+
+               break;
+       }
+
+       write_unlock(&resource_lock);
+       free_resource(new_res);
+       return ret;
+}
+#endif /* CONFIG_MEMORY_HOTREMOVE */
+
 /*
  * Managed region resource
  */