sh: convert /proc/cpu/aligmnent, /proc/cpu/kernel_alignment to seq_file
[safe/jmp/linux-2.6] / kernel / resource.c
index 414d6fc..fb11a58 100644 (file)
@@ -17,6 +17,7 @@
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 #include <linux/device.h>
+#include <linux/pfn.h>
 #include <asm/io.h>
 
 
@@ -38,10 +39,6 @@ EXPORT_SYMBOL(iomem_resource);
 
 static DEFINE_RWLOCK(resource_lock);
 
-#ifdef CONFIG_PROC_FS
-
-enum { MAX_IORES_LEVEL = 5 };
-
 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
 {
        struct resource *p = v;
@@ -53,6 +50,10 @@ static void *r_next(struct seq_file *m, void *v, loff_t *pos)
        return p->sibling;
 }
 
+#ifdef CONFIG_PROC_FS
+
+enum { MAX_IORES_LEVEL = 5 };
+
 static void *r_start(struct seq_file *m, loff_t *pos)
        __acquires(resource_lock)
 {
@@ -222,13 +223,13 @@ int release_resource(struct resource *old)
 
 EXPORT_SYMBOL(release_resource);
 
-#if defined(CONFIG_MEMORY_HOTPLUG) && !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
+#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
 /*
  * Finds the lowest memory reosurce exists within [res->start.res->end)
- * the caller must specify res->start, res->end, res->flags.
+ * the caller must specify res->start, res->end, res->flags and "name".
  * If found, returns 0, res is overwritten, if not found, returns -1.
  */
-static int find_next_system_ram(struct resource *res)
+static int find_next_system_ram(struct resource *res, char *name)
 {
        resource_size_t start, end;
        struct resource *p;
@@ -244,6 +245,8 @@ static int find_next_system_ram(struct resource *res)
                /* system ram is just marked as IORESOURCE_MEM */
                if (p->flags != res->flags)
                        continue;
+               if (name && strcmp(p->name, name))
+                       continue;
                if (p->start > end) {
                        p = NULL;
                        break;
@@ -261,19 +264,26 @@ static int find_next_system_ram(struct resource *res)
                res->end = p->end;
        return 0;
 }
-int
-walk_memory_resource(unsigned long start_pfn, unsigned long nr_pages, void *arg,
-                       int (*func)(unsigned long, unsigned long, void *))
+
+/*
+ * This function calls callback against all memory range of "System RAM"
+ * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
+ * Now, this function is only for "System RAM".
+ */
+int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
+               void *arg, int (*func)(unsigned long, unsigned long, void *))
 {
        struct resource res;
        unsigned long pfn, len;
        u64 orig_end;
        int ret = -1;
+
        res.start = (u64) start_pfn << PAGE_SHIFT;
        res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
        res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
        orig_end = res.end;
-       while ((res.start < res.end) && (find_next_system_ram(&res) >= 0)) {
+       while ((res.start < res.end) &&
+               (find_next_system_ram(&res, "System RAM") >= 0)) {
                pfn = (unsigned long)(res.start >> PAGE_SHIFT);
                len = (unsigned long)((res.end + 1 - res.start) >> PAGE_SHIFT);
                ret = (*func)(pfn, len, arg);
@@ -522,7 +532,7 @@ static void __init __reserve_region_with_split(struct resource *root,
 {
        struct resource *parent = root;
        struct resource *conflict;
-       struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
+       struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
 
        if (!res)
                return;
@@ -532,50 +542,24 @@ static void __init __reserve_region_with_split(struct resource *root,
        res->end = end;
        res->flags = IORESOURCE_BUSY;
 
-       for (;;) {
-               conflict = __request_resource(parent, res);
-               if (!conflict)
-                       break;
-               if (conflict != parent) {
-                       parent = conflict;
-                       if (!(conflict->flags & IORESOURCE_BUSY))
-                               continue;
-               }
-
-               /* Uhhuh, that didn't work out.. */
-               kfree(res);
-               res = NULL;
-               break;
-       }
-
-       if (!res) {
-               printk(KERN_DEBUG "    __reserve_region_with_split: (%s) [%llx, %llx], res: (%s) [%llx, %llx]\n",
-                        conflict->name, conflict->start, conflict->end,
-                        name, start, end);
-
-               /* failed, split and try again */
-
-               /* conflict coverred whole area */
-               if (conflict->start <= start && conflict->end >= end)
-                       return;
+       conflict = __request_resource(parent, res);
+       if (!conflict)
+               return;
 
-               if (conflict->start > start)
-                       __reserve_region_with_split(root, start, conflict->start-1, name);
-               if (!(conflict->flags & IORESOURCE_BUSY)) {
-                       resource_size_t common_start, common_end;
+       /* failed, split and try again */
+       kfree(res);
 
-                       common_start = max(conflict->start, start);
-                       common_end = min(conflict->end, end);
-                       if (common_start < common_end)
-                               __reserve_region_with_split(root, common_start, common_end, name);
-               }
-               if (conflict->end < end)
-                       __reserve_region_with_split(root, conflict->end+1, end, name);
-       }
+       /* conflict covered whole area */
+       if (conflict->start <= start && conflict->end >= end)
+               return;
 
+       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 reserve_region_with_split(struct resource *root,
+void __init reserve_region_with_split(struct resource *root,
                resource_size_t start, resource_size_t end,
                const char *name)
 {
@@ -623,40 +607,43 @@ resource_size_t resource_alignment(struct resource *res)
  * @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)
+                                  const char *name, int flags)
 {
        struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
 
-       if (res) {
-               res->name = name;
-               res->start = start;
-               res->end = start + n - 1;
-               res->flags = IORESOURCE_BUSY;
+       if (!res)
+               return NULL;
 
-               write_lock(&resource_lock);
+       res->name = name;
+       res->start = start;
+       res->end = start + n - 1;
+       res->flags = IORESOURCE_BUSY;
+       res->flags |= flags;
 
-               for (;;) {
-                       struct resource *conflict;
+       write_lock(&resource_lock);
 
-                       conflict = __request_resource(parent, res);
-                       if (!conflict)
-                               break;
-                       if (conflict != parent) {
-                               parent = conflict;
-                               if (!(conflict->flags & IORESOURCE_BUSY))
-                                       continue;
-                       }
+       for (;;) {
+               struct resource *conflict;
 
-                       /* Uhhuh, that didn't work out.. */
-                       kfree(res);
-                       res = NULL;
+               conflict = __request_resource(parent, res);
+               if (!conflict)
                        break;
+               if (conflict != parent) {
+                       parent = conflict;
+                       if (!(conflict->flags & IORESOURCE_BUSY))
+                               continue;
                }
-               write_unlock(&resource_lock);
+
+               /* Uhhuh, that didn't work out.. */
+               kfree(res);
+               res = NULL;
+               break;
        }
+       write_unlock(&resource_lock);
        return res;
 }
 EXPORT_SYMBOL(__request_region);
@@ -681,7 +668,7 @@ int __check_region(struct resource *parent, resource_size_t start,
 {
        struct resource * res;
 
-       res = __request_region(parent, start, n, "check-region");
+       res = __request_region(parent, start, n, "check-region", 0);
        if (!res)
                return -EBUSY;
 
@@ -778,7 +765,7 @@ struct resource * __devm_request_region(struct device *dev,
        dr->start = start;
        dr->n = n;
 
-       res = __request_region(parent, start, n, name);
+       res = __request_region(parent, start, n, name, 0);
        if (res)
                devres_add(dev, dr);
        else
@@ -809,7 +796,7 @@ static int __init reserve_setup(char *str)
        static struct resource reserve[MAXRESERVE];
 
        for (;;) {
-               int io_start, io_num;
+               unsigned int io_start, io_num;
                int x = reserved;
 
                if (get_option (&str, &io_start) != 2)
@@ -831,3 +818,104 @@ static int __init reserve_setup(char *str)
 }
 
 __setup("reserve=", reserve_setup);
+
+/*
+ * Check if the requested addr and size spans more than any slot in the
+ * iomem resource tree.
+ */
+int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
+{
+       struct resource *p = &iomem_resource;
+       int err = 0;
+       loff_t l;
+
+       read_lock(&resource_lock);
+       for (p = p->child; p ; p = r_next(NULL, p, &l)) {
+               /*
+                * We can probably skip the resources without
+                * IORESOURCE_IO attribute?
+                */
+               if (p->start >= addr + size)
+                       continue;
+               if (p->end < addr)
+                       continue;
+               if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
+                   PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
+                       continue;
+               /*
+                * if a resource is "BUSY", it's not a hardware resource
+                * but a driver mapping of such a resource; we don't want
+                * to warn for those; some drivers legitimately map only
+                * partial hardware resources. (example: vesafb)
+                */
+               if (p->flags & IORESOURCE_BUSY)
+                       continue;
+
+               printk(KERN_WARNING "resource map sanity check conflict: "
+                      "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
+                      (unsigned long long)addr,
+                      (unsigned long long)(addr + size - 1),
+                      (unsigned long long)p->start,
+                      (unsigned long long)p->end,
+                      p->name);
+               err = -1;
+               break;
+       }
+       read_unlock(&resource_lock);
+
+       return err;
+}
+
+#ifdef CONFIG_STRICT_DEVMEM
+static int strict_iomem_checks = 1;
+#else
+static int strict_iomem_checks;
+#endif
+
+/*
+ * check if an address is reserved in the iomem resource tree
+ * returns 1 if reserved, 0 if not reserved.
+ */
+int iomem_is_exclusive(u64 addr)
+{
+       struct resource *p = &iomem_resource;
+       int err = 0;
+       loff_t l;
+       int size = PAGE_SIZE;
+
+       if (!strict_iomem_checks)
+               return 0;
+
+       addr = addr & PAGE_MASK;
+
+       read_lock(&resource_lock);
+       for (p = p->child; p ; p = r_next(NULL, p, &l)) {
+               /*
+                * We can probably skip the resources without
+                * IORESOURCE_IO attribute?
+                */
+               if (p->start >= addr + size)
+                       break;
+               if (p->end < addr)
+                       continue;
+               if (p->flags & IORESOURCE_BUSY &&
+                    p->flags & IORESOURCE_EXCLUSIVE) {
+                       err = 1;
+                       break;
+               }
+       }
+       read_unlock(&resource_lock);
+
+       return err;
+}
+
+static int __init strict_iomem(char *str)
+{
+       if (strstr(str, "relaxed"))
+               strict_iomem_checks = 0;
+       if (strstr(str, "strict"))
+               strict_iomem_checks = 1;
+       return 1;
+}
+
+__setup("iomem=", strict_iomem);