fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / kernel / resource.c
index e3080fc..7b9a497 100644 (file)
@@ -7,7 +7,6 @@
  * Arbitrary resource management.
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/sched.h>
 #include <linux/errno.h>
 
 struct resource ioport_resource = {
        .name   = "PCI IO",
-       .start  = 0x0000,
+       .start  = 0,
        .end    = IO_SPACE_LIMIT,
        .flags  = IORESOURCE_IO,
 };
-
 EXPORT_SYMBOL(ioport_resource);
 
 struct resource iomem_resource = {
        .name   = "PCI mem",
-       .start  = 0UL,
-       .end    = ~0UL,
+       .start  = 0,
+       .end    = -1,
        .flags  = IORESOURCE_MEM,
 };
-
 EXPORT_SYMBOL(iomem_resource);
 
 static DEFINE_RWLOCK(resource_lock);
@@ -83,15 +80,15 @@ static int r_show(struct seq_file *m, void *v)
        for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
                if (p->parent == root)
                        break;
-       seq_printf(m, "%*s%0*lx-%0*lx : %s\n",
+       seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
                        depth * 2, "",
-                       width, r->start,
-                       width, r->end,
+                       width, (unsigned long long) r->start,
+                       width, (unsigned long long) r->end,
                        r->name ? r->name : "<BAD>");
        return 0;
 }
 
-static struct seq_operations resource_op = {
+static const struct seq_operations resource_op = {
        .start  = r_start,
        .next   = r_next,
        .stop   = r_stop,
@@ -118,14 +115,14 @@ static int iomem_open(struct inode *inode, struct file *file)
        return res;
 }
 
-static struct file_operations proc_ioports_operations = {
+static const struct file_operations proc_ioports_operations = {
        .open           = ioports_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
        .release        = seq_release,
 };
 
-static struct file_operations proc_iomem_operations = {
+static const struct file_operations proc_iomem_operations = {
        .open           = iomem_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
@@ -151,8 +148,8 @@ __initcall(ioresources_init);
 /* Return the conflict entry if you can't request it */
 static struct resource * __request_resource(struct resource *root, struct resource *new)
 {
-       unsigned long start = new->start;
-       unsigned long end = new->end;
+       resource_size_t start = new->start;
+       resource_size_t end = new->end;
        struct resource *tmp, **p;
 
        if (end < start)
@@ -196,6 +193,13 @@ static int __release_resource(struct resource *old)
        return -EINVAL;
 }
 
+/**
+ * request_resource - request and reserve an I/O or memory resource
+ * @root: root resource descriptor
+ * @new: resource descriptor desired by caller
+ *
+ * Returns 0 for success, negative error code on error.
+ */
 int request_resource(struct resource *root, struct resource *new)
 {
        struct resource *conflict;
@@ -208,6 +212,15 @@ int request_resource(struct resource *root, struct resource *new)
 
 EXPORT_SYMBOL(request_resource);
 
+/**
+ * ____request_resource - reserve a resource, with resource conflict returned
+ * @root: root resource descriptor
+ * @new: resource descriptor desired by caller
+ *
+ * Returns:
+ * On success, NULL is returned.
+ * On error, a pointer to the conflicting resource is returned.
+ */
 struct resource *____request_resource(struct resource *root, struct resource *new)
 {
        struct resource *conflict;
@@ -220,6 +233,10 @@ struct resource *____request_resource(struct resource *root, struct resource *ne
 
 EXPORT_SYMBOL(____request_resource);
 
+/**
+ * release_resource - release a previously reserved resource
+ * @old: resource pointer
+ */
 int release_resource(struct resource *old)
 {
        int retval;
@@ -232,15 +249,55 @@ int release_resource(struct resource *old)
 
 EXPORT_SYMBOL(release_resource);
 
+#ifdef CONFIG_MEMORY_HOTPLUG
+/*
+ * Finds the lowest memory reosurce exists within [res->start.res->end)
+ * the caller must specify res->start, res->end, res->flags.
+ * If found, returns 0, res is overwritten, if not found, returns -1.
+ */
+int find_next_system_ram(struct resource *res)
+{
+       resource_size_t start, end;
+       struct resource *p;
+
+       BUG_ON(!res);
+
+       start = res->start;
+       end = res->end;
+       BUG_ON(start >= end);
+
+       read_lock(&resource_lock);
+       for (p = iomem_resource.child; p ; p = p->sibling) {
+               /* system ram is just marked as IORESOURCE_MEM */
+               if (p->flags != res->flags)
+                       continue;
+               if (p->start > end) {
+                       p = NULL;
+                       break;
+               }
+               if ((p->end >= start) && (p->start < end))
+                       break;
+       }
+       read_unlock(&resource_lock);
+       if (!p)
+               return -1;
+       /* copy data */
+       if (res->start < p->start)
+               res->start = p->start;
+       if (res->end > p->end)
+               res->end = p->end;
+       return 0;
+}
+#endif
+
 /*
  * Find empty slot in the resource tree given range and alignment.
  */
 static int find_resource(struct resource *root, struct resource *new,
-                        unsigned long size,
-                        unsigned long min, unsigned long max,
-                        unsigned long align,
+                        resource_size_t size, resource_size_t min,
+                        resource_size_t max, resource_size_t align,
                         void (*alignf)(void *, struct resource *,
-                                       unsigned long, unsigned long),
+                                       resource_size_t, resource_size_t),
                         void *alignf_data)
 {
        struct resource *this = root->child;
@@ -278,15 +335,22 @@ static int find_resource(struct resource *root, struct resource *new,
        return -EBUSY;
 }
 
-/*
- * Allocate empty slot in the resource tree given range and alignment.
+/**
+ * allocate_resource - allocate empty slot in the resource tree given range & alignment
+ * @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
+ * @align: alignment requested, in bytes
+ * @alignf: alignment function, optional, called if not NULL
+ * @alignf_data: arbitrary data to pass to the @alignf function
  */
 int allocate_resource(struct resource *root, struct resource *new,
-                     unsigned long size,
-                     unsigned long min, unsigned long max,
-                     unsigned long align,
+                     resource_size_t size, resource_size_t min,
+                     resource_size_t max, resource_size_t align,
                      void (*alignf)(void *, struct resource *,
-                                    unsigned long, unsigned long),
+                                    resource_size_t, resource_size_t),
                      void *alignf_data)
 {
        int err;
@@ -308,12 +372,11 @@ EXPORT_SYMBOL(allocate_resource);
  *
  * Returns 0 on success, -EBUSY if the resource can't be inserted.
  *
- * This function is equivalent of request_resource when no conflict
+ * This function is equivalent to request_resource when no conflict
  * happens. If a conflict happens, and the conflicting resources
  * entirely fit within the range of the new resource, then the new
- * resource is inserted and the conflicting resources become childs of
- * the new resource.  Otherwise the new resource becomes the child of
- * the conflicting resource
+ * resource is inserted and the conflicting resources become children of
+ * the new resource.
  */
 int insert_resource(struct resource *parent, struct resource *new)
 {
@@ -321,20 +384,21 @@ int insert_resource(struct resource *parent, struct resource *new)
        struct resource *first, *next;
 
        write_lock(&resource_lock);
- begin:
-       result = 0;
-       first = __request_resource(parent, new);
-       if (!first)
-               goto out;
 
-       result = -EBUSY;
-       if (first == parent)
-               goto out;
+       for (;; parent = first) {
+               result = 0;
+               first = __request_resource(parent, new);
+               if (!first)
+                       goto out;
+
+               result = -EBUSY;
+               if (first == parent)
+                       goto out;
 
-       /* Resource fully contained by the clashing resource? Recurse into it */
-       if (first->start <= new->start && first->end >= new->end) {
-               parent = first;
-               goto begin;
+               if ((first->start > new->start) || (first->end < new->end))
+                       break;
+               if ((first->start == new->start) && (first->end == new->end))
+                       break;
        }
 
        for (next = first; ; next = next->sibling) {
@@ -371,17 +435,20 @@ int insert_resource(struct resource *parent, struct resource *new)
        return result;
 }
 
-EXPORT_SYMBOL(insert_resource);
-
-/*
+/**
+ * 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 -EBUSY if it can't fit.  Existing children of
- * the resource are assumed to be immutable.
+ * 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, unsigned long start, unsigned long size)
+int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
 {
        struct resource *tmp, *parent = res->parent;
-       unsigned long end = start + size - 1;
+       resource_size_t end = start + size - 1;
        int result = -EBUSY;
 
        write_lock(&resource_lock);
@@ -422,13 +489,23 @@ EXPORT_SYMBOL(adjust_resource);
  * Note how this, unlike the above, knows about
  * the IO flag meanings (busy etc).
  *
- * Request-region creates a new busy region.
+ * request_region creates a new busy region.
  *
- * Check-region returns non-zero if the area is already busy
+ * check_region returns non-zero if the area is already busy.
  *
- * Release-region releases a matching busy region.
+ * release_region releases a matching busy region.
  */
-struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name)
+
+/**
+ * __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
+ */
+struct resource * __request_region(struct resource *parent,
+                                  resource_size_t start, resource_size_t n,
+                                  const char *name)
 {
        struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
 
@@ -461,10 +538,25 @@ struct resource * __request_region(struct resource *parent, unsigned long start,
        }
        return res;
 }
-
 EXPORT_SYMBOL(__request_region);
 
-int __check_region(struct resource *parent, unsigned long start, unsigned long n)
+/**
+ * __check_region - check if a resource region is busy or free
+ * @parent: parent resource descriptor
+ * @start: resource start address
+ * @n: resource region size
+ *
+ * Returns 0 if the region is free at the moment it is checked,
+ * returns %-EBUSY if the region is busy.
+ *
+ * NOTE:
+ * This function is deprecated because its use is racy.
+ * Even if it returns 0, a subsequent call to request_region()
+ * may fail because another driver etc. just allocated the region.
+ * Do NOT use it.  It will be removed from the kernel.
+ */
+int __check_region(struct resource *parent, resource_size_t start,
+                       resource_size_t n)
 {
        struct resource * res;
 
@@ -476,13 +568,21 @@ int __check_region(struct resource *parent, unsigned long start, unsigned long n
        kfree(res);
        return 0;
 }
-
 EXPORT_SYMBOL(__check_region);
 
-void __release_region(struct resource *parent, unsigned long start, unsigned long n)
+/**
+ * __release_region - release a previously reserved resource region
+ * @parent: parent resource descriptor
+ * @start: resource start address
+ * @n: resource region size
+ *
+ * The described resource region must match a currently busy region.
+ */
+void __release_region(struct resource *parent, resource_size_t start,
+                       resource_size_t n)
 {
        struct resource **p;
-       unsigned long end;
+       resource_size_t end;
 
        p = &parent->child;
        end = start + n - 1;
@@ -511,9 +611,10 @@ void __release_region(struct resource *parent, unsigned long start, unsigned lon
 
        write_unlock(&resource_lock);
 
-       printk(KERN_WARNING "Trying to free nonexistent resource <%08lx-%08lx>\n", start, end);
+       printk(KERN_WARNING "Trying to free nonexistent resource "
+               "<%016llx-%016llx>\n", (unsigned long long)start,
+               (unsigned long long)end);
 }
-
 EXPORT_SYMBOL(__release_region);
 
 /*