Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/kernel/resource.c
0003  *
0004  * Copyright (C) 1999   Linus Torvalds
0005  * Copyright (C) 1999   Martin Mares <mj@ucw.cz>
0006  *
0007  * Arbitrary resource management.
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/export.h>
0013 #include <linux/errno.h>
0014 #include <linux/ioport.h>
0015 #include <linux/init.h>
0016 #include <linux/slab.h>
0017 #include <linux/spinlock.h>
0018 #include <linux/fs.h>
0019 #include <linux/proc_fs.h>
0020 #include <linux/sched.h>
0021 #include <linux/seq_file.h>
0022 #include <linux/device.h>
0023 #include <linux/pfn.h>
0024 #include <linux/mm.h>
0025 #include <linux/resource_ext.h>
0026 #include <asm/io.h>
0027 
0028 
0029 struct resource ioport_resource = {
0030     .name   = "PCI IO",
0031     .start  = 0,
0032     .end    = IO_SPACE_LIMIT,
0033     .flags  = IORESOURCE_IO,
0034 };
0035 EXPORT_SYMBOL(ioport_resource);
0036 
0037 struct resource iomem_resource = {
0038     .name   = "PCI mem",
0039     .start  = 0,
0040     .end    = -1,
0041     .flags  = IORESOURCE_MEM,
0042 };
0043 EXPORT_SYMBOL(iomem_resource);
0044 
0045 /* constraints to be met while allocating resources */
0046 struct resource_constraint {
0047     resource_size_t min, max, align;
0048     resource_size_t (*alignf)(void *, const struct resource *,
0049             resource_size_t, resource_size_t);
0050     void *alignf_data;
0051 };
0052 
0053 static DEFINE_RWLOCK(resource_lock);
0054 
0055 /*
0056  * For memory hotplug, there is no way to free resource entries allocated
0057  * by boot mem after the system is up. So for reusing the resource entry
0058  * we need to remember the resource.
0059  */
0060 static struct resource *bootmem_resource_free;
0061 static DEFINE_SPINLOCK(bootmem_resource_lock);
0062 
0063 static struct resource *next_resource(struct resource *p, bool sibling_only)
0064 {
0065     /* Caller wants to traverse through siblings only */
0066     if (sibling_only)
0067         return p->sibling;
0068 
0069     if (p->child)
0070         return p->child;
0071     while (!p->sibling && p->parent)
0072         p = p->parent;
0073     return p->sibling;
0074 }
0075 
0076 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
0077 {
0078     struct resource *p = v;
0079     (*pos)++;
0080     return (void *)next_resource(p, false);
0081 }
0082 
0083 #ifdef CONFIG_PROC_FS
0084 
0085 enum { MAX_IORES_LEVEL = 5 };
0086 
0087 static void *r_start(struct seq_file *m, loff_t *pos)
0088     __acquires(resource_lock)
0089 {
0090     struct resource *p = m->private;
0091     loff_t l = 0;
0092     read_lock(&resource_lock);
0093     for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
0094         ;
0095     return p;
0096 }
0097 
0098 static void r_stop(struct seq_file *m, void *v)
0099     __releases(resource_lock)
0100 {
0101     read_unlock(&resource_lock);
0102 }
0103 
0104 static int r_show(struct seq_file *m, void *v)
0105 {
0106     struct resource *root = m->private;
0107     struct resource *r = v, *p;
0108     unsigned long long start, end;
0109     int width = root->end < 0x10000 ? 4 : 8;
0110     int depth;
0111 
0112     for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
0113         if (p->parent == root)
0114             break;
0115 
0116     if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
0117         start = r->start;
0118         end = r->end;
0119     } else {
0120         start = end = 0;
0121     }
0122 
0123     seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
0124             depth * 2, "",
0125             width, start,
0126             width, end,
0127             r->name ? r->name : "<BAD>");
0128     return 0;
0129 }
0130 
0131 static const struct seq_operations resource_op = {
0132     .start  = r_start,
0133     .next   = r_next,
0134     .stop   = r_stop,
0135     .show   = r_show,
0136 };
0137 
0138 static int ioports_open(struct inode *inode, struct file *file)
0139 {
0140     int res = seq_open(file, &resource_op);
0141     if (!res) {
0142         struct seq_file *m = file->private_data;
0143         m->private = &ioport_resource;
0144     }
0145     return res;
0146 }
0147 
0148 static int iomem_open(struct inode *inode, struct file *file)
0149 {
0150     int res = seq_open(file, &resource_op);
0151     if (!res) {
0152         struct seq_file *m = file->private_data;
0153         m->private = &iomem_resource;
0154     }
0155     return res;
0156 }
0157 
0158 static const struct file_operations proc_ioports_operations = {
0159     .open       = ioports_open,
0160     .read       = seq_read,
0161     .llseek     = seq_lseek,
0162     .release    = seq_release,
0163 };
0164 
0165 static const struct file_operations proc_iomem_operations = {
0166     .open       = iomem_open,
0167     .read       = seq_read,
0168     .llseek     = seq_lseek,
0169     .release    = seq_release,
0170 };
0171 
0172 static int __init ioresources_init(void)
0173 {
0174     proc_create("ioports", 0, NULL, &proc_ioports_operations);
0175     proc_create("iomem", 0, NULL, &proc_iomem_operations);
0176     return 0;
0177 }
0178 __initcall(ioresources_init);
0179 
0180 #endif /* CONFIG_PROC_FS */
0181 
0182 static void free_resource(struct resource *res)
0183 {
0184     if (!res)
0185         return;
0186 
0187     if (!PageSlab(virt_to_head_page(res))) {
0188         spin_lock(&bootmem_resource_lock);
0189         res->sibling = bootmem_resource_free;
0190         bootmem_resource_free = res;
0191         spin_unlock(&bootmem_resource_lock);
0192     } else {
0193         kfree(res);
0194     }
0195 }
0196 
0197 static struct resource *alloc_resource(gfp_t flags)
0198 {
0199     struct resource *res = NULL;
0200 
0201     spin_lock(&bootmem_resource_lock);
0202     if (bootmem_resource_free) {
0203         res = bootmem_resource_free;
0204         bootmem_resource_free = res->sibling;
0205     }
0206     spin_unlock(&bootmem_resource_lock);
0207 
0208     if (res)
0209         memset(res, 0, sizeof(struct resource));
0210     else
0211         res = kzalloc(sizeof(struct resource), flags);
0212 
0213     return res;
0214 }
0215 
0216 /* Return the conflict entry if you can't request it */
0217 static struct resource * __request_resource(struct resource *root, struct resource *new)
0218 {
0219     resource_size_t start = new->start;
0220     resource_size_t end = new->end;
0221     struct resource *tmp, **p;
0222 
0223     if (end < start)
0224         return root;
0225     if (start < root->start)
0226         return root;
0227     if (end > root->end)
0228         return root;
0229     p = &root->child;
0230     for (;;) {
0231         tmp = *p;
0232         if (!tmp || tmp->start > end) {
0233             new->sibling = tmp;
0234             *p = new;
0235             new->parent = root;
0236             return NULL;
0237         }
0238         p = &tmp->sibling;
0239         if (tmp->end < start)
0240             continue;
0241         return tmp;
0242     }
0243 }
0244 
0245 static int __release_resource(struct resource *old, bool release_child)
0246 {
0247     struct resource *tmp, **p, *chd;
0248 
0249     p = &old->parent->child;
0250     for (;;) {
0251         tmp = *p;
0252         if (!tmp)
0253             break;
0254         if (tmp == old) {
0255             if (release_child || !(tmp->child)) {
0256                 *p = tmp->sibling;
0257             } else {
0258                 for (chd = tmp->child;; chd = chd->sibling) {
0259                     chd->parent = tmp->parent;
0260                     if (!(chd->sibling))
0261                         break;
0262                 }
0263                 *p = tmp->child;
0264                 chd->sibling = tmp->sibling;
0265             }
0266             old->parent = NULL;
0267             return 0;
0268         }
0269         p = &tmp->sibling;
0270     }
0271     return -EINVAL;
0272 }
0273 
0274 static void __release_child_resources(struct resource *r)
0275 {
0276     struct resource *tmp, *p;
0277     resource_size_t size;
0278 
0279     p = r->child;
0280     r->child = NULL;
0281     while (p) {
0282         tmp = p;
0283         p = p->sibling;
0284 
0285         tmp->parent = NULL;
0286         tmp->sibling = NULL;
0287         __release_child_resources(tmp);
0288 
0289         printk(KERN_DEBUG "release child resource %pR\n", tmp);
0290         /* need to restore size, and keep flags */
0291         size = resource_size(tmp);
0292         tmp->start = 0;
0293         tmp->end = size - 1;
0294     }
0295 }
0296 
0297 void release_child_resources(struct resource *r)
0298 {
0299     write_lock(&resource_lock);
0300     __release_child_resources(r);
0301     write_unlock(&resource_lock);
0302 }
0303 
0304 /**
0305  * request_resource_conflict - request and reserve an I/O or memory resource
0306  * @root: root resource descriptor
0307  * @new: resource descriptor desired by caller
0308  *
0309  * Returns 0 for success, conflict resource on error.
0310  */
0311 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
0312 {
0313     struct resource *conflict;
0314 
0315     write_lock(&resource_lock);
0316     conflict = __request_resource(root, new);
0317     write_unlock(&resource_lock);
0318     return conflict;
0319 }
0320 
0321 /**
0322  * request_resource - request and reserve an I/O or memory resource
0323  * @root: root resource descriptor
0324  * @new: resource descriptor desired by caller
0325  *
0326  * Returns 0 for success, negative error code on error.
0327  */
0328 int request_resource(struct resource *root, struct resource *new)
0329 {
0330     struct resource *conflict;
0331 
0332     conflict = request_resource_conflict(root, new);
0333     return conflict ? -EBUSY : 0;
0334 }
0335 
0336 EXPORT_SYMBOL(request_resource);
0337 
0338 /**
0339  * release_resource - release a previously reserved resource
0340  * @old: resource pointer
0341  */
0342 int release_resource(struct resource *old)
0343 {
0344     int retval;
0345 
0346     write_lock(&resource_lock);
0347     retval = __release_resource(old, true);
0348     write_unlock(&resource_lock);
0349     return retval;
0350 }
0351 
0352 EXPORT_SYMBOL(release_resource);
0353 
0354 /*
0355  * Finds the lowest iomem resource existing within [res->start.res->end).
0356  * The caller must specify res->start, res->end, res->flags, and optionally
0357  * desc.  If found, returns 0, res is overwritten, if not found, returns -1.
0358  * This function walks the whole tree and not just first level children until
0359  * and unless first_level_children_only is true.
0360  */
0361 static int find_next_iomem_res(struct resource *res, unsigned long desc,
0362                    bool first_level_children_only)
0363 {
0364     resource_size_t start, end;
0365     struct resource *p;
0366     bool sibling_only = false;
0367 
0368     BUG_ON(!res);
0369 
0370     start = res->start;
0371     end = res->end;
0372     BUG_ON(start >= end);
0373 
0374     if (first_level_children_only)
0375         sibling_only = true;
0376 
0377     read_lock(&resource_lock);
0378 
0379     for (p = iomem_resource.child; p; p = next_resource(p, sibling_only)) {
0380         if ((p->flags & res->flags) != res->flags)
0381             continue;
0382         if ((desc != IORES_DESC_NONE) && (desc != p->desc))
0383             continue;
0384         if (p->start > end) {
0385             p = NULL;
0386             break;
0387         }
0388         if ((p->end >= start) && (p->start < end))
0389             break;
0390     }
0391 
0392     read_unlock(&resource_lock);
0393     if (!p)
0394         return -1;
0395     /* copy data */
0396     if (res->start < p->start)
0397         res->start = p->start;
0398     if (res->end > p->end)
0399         res->end = p->end;
0400     return 0;
0401 }
0402 
0403 /*
0404  * Walks through iomem resources and calls func() with matching resource
0405  * ranges. This walks through whole tree and not just first level children.
0406  * All the memory ranges which overlap start,end and also match flags and
0407  * desc are valid candidates.
0408  *
0409  * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
0410  * @flags: I/O resource flags
0411  * @start: start addr
0412  * @end: end addr
0413  *
0414  * NOTE: For a new descriptor search, define a new IORES_DESC in
0415  * <linux/ioport.h> and set it in 'desc' of a target resource entry.
0416  */
0417 int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
0418         u64 end, void *arg, int (*func)(u64, u64, void *))
0419 {
0420     struct resource res;
0421     u64 orig_end;
0422     int ret = -1;
0423 
0424     res.start = start;
0425     res.end = end;
0426     res.flags = flags;
0427     orig_end = res.end;
0428 
0429     while ((res.start < res.end) &&
0430         (!find_next_iomem_res(&res, desc, false))) {
0431 
0432         ret = (*func)(res.start, res.end, arg);
0433         if (ret)
0434             break;
0435 
0436         res.start = res.end + 1;
0437         res.end = orig_end;
0438     }
0439 
0440     return ret;
0441 }
0442 
0443 /*
0444  * This function calls the @func callback against all memory ranges of type
0445  * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
0446  * Now, this function is only for System RAM, it deals with full ranges and
0447  * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
0448  * ranges.
0449  */
0450 int walk_system_ram_res(u64 start, u64 end, void *arg,
0451                 int (*func)(u64, u64, void *))
0452 {
0453     struct resource res;
0454     u64 orig_end;
0455     int ret = -1;
0456 
0457     res.start = start;
0458     res.end = end;
0459     res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
0460     orig_end = res.end;
0461     while ((res.start < res.end) &&
0462         (!find_next_iomem_res(&res, IORES_DESC_NONE, true))) {
0463         ret = (*func)(res.start, res.end, arg);
0464         if (ret)
0465             break;
0466         res.start = res.end + 1;
0467         res.end = orig_end;
0468     }
0469     return ret;
0470 }
0471 
0472 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
0473 
0474 /*
0475  * This function calls the @func callback against all memory ranges of type
0476  * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
0477  * It is to be used only for System RAM.
0478  */
0479 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
0480         void *arg, int (*func)(unsigned long, unsigned long, void *))
0481 {
0482     struct resource res;
0483     unsigned long pfn, end_pfn;
0484     u64 orig_end;
0485     int ret = -1;
0486 
0487     res.start = (u64) start_pfn << PAGE_SHIFT;
0488     res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
0489     res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
0490     orig_end = res.end;
0491     while ((res.start < res.end) &&
0492         (find_next_iomem_res(&res, IORES_DESC_NONE, true) >= 0)) {
0493         pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
0494         end_pfn = (res.end + 1) >> PAGE_SHIFT;
0495         if (end_pfn > pfn)
0496             ret = (*func)(pfn, end_pfn - pfn, arg);
0497         if (ret)
0498             break;
0499         res.start = res.end + 1;
0500         res.end = orig_end;
0501     }
0502     return ret;
0503 }
0504 
0505 #endif
0506 
0507 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
0508 {
0509     return 1;
0510 }
0511 /*
0512  * This generic page_is_ram() returns true if specified address is
0513  * registered as System RAM in iomem_resource list.
0514  */
0515 int __weak page_is_ram(unsigned long pfn)
0516 {
0517     return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
0518 }
0519 EXPORT_SYMBOL_GPL(page_is_ram);
0520 
0521 /**
0522  * region_intersects() - determine intersection of region with known resources
0523  * @start: region start address
0524  * @size: size of region
0525  * @flags: flags of resource (in iomem_resource)
0526  * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
0527  *
0528  * Check if the specified region partially overlaps or fully eclipses a
0529  * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
0530  * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
0531  * return REGION_MIXED if the region overlaps @flags/@desc and another
0532  * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
0533  * and no other defined resource. Note that REGION_INTERSECTS is also
0534  * returned in the case when the specified region overlaps RAM and undefined
0535  * memory holes.
0536  *
0537  * region_intersect() is used by memory remapping functions to ensure
0538  * the user is not remapping RAM and is a vast speed up over walking
0539  * through the resource table page by page.
0540  */
0541 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
0542               unsigned long desc)
0543 {
0544     resource_size_t end = start + size - 1;
0545     int type = 0; int other = 0;
0546     struct resource *p;
0547 
0548     read_lock(&resource_lock);
0549     for (p = iomem_resource.child; p ; p = p->sibling) {
0550         bool is_type = (((p->flags & flags) == flags) &&
0551                 ((desc == IORES_DESC_NONE) ||
0552                  (desc == p->desc)));
0553 
0554         if (start >= p->start && start <= p->end)
0555             is_type ? type++ : other++;
0556         if (end >= p->start && end <= p->end)
0557             is_type ? type++ : other++;
0558         if (p->start >= start && p->end <= end)
0559             is_type ? type++ : other++;
0560     }
0561     read_unlock(&resource_lock);
0562 
0563     if (other == 0)
0564         return type ? REGION_INTERSECTS : REGION_DISJOINT;
0565 
0566     if (type)
0567         return REGION_MIXED;
0568 
0569     return REGION_DISJOINT;
0570 }
0571 EXPORT_SYMBOL_GPL(region_intersects);
0572 
0573 void __weak arch_remove_reservations(struct resource *avail)
0574 {
0575 }
0576 
0577 static resource_size_t simple_align_resource(void *data,
0578                          const struct resource *avail,
0579                          resource_size_t size,
0580                          resource_size_t align)
0581 {
0582     return avail->start;
0583 }
0584 
0585 static void resource_clip(struct resource *res, resource_size_t min,
0586               resource_size_t max)
0587 {
0588     if (res->start < min)
0589         res->start = min;
0590     if (res->end > max)
0591         res->end = max;
0592 }
0593 
0594 /*
0595  * Find empty slot in the resource tree with the given range and
0596  * alignment constraints
0597  */
0598 static int __find_resource(struct resource *root, struct resource *old,
0599              struct resource *new,
0600              resource_size_t  size,
0601              struct resource_constraint *constraint)
0602 {
0603     struct resource *this = root->child;
0604     struct resource tmp = *new, avail, alloc;
0605 
0606     tmp.start = root->start;
0607     /*
0608      * Skip past an allocated resource that starts at 0, since the assignment
0609      * of this->start - 1 to tmp->end below would cause an underflow.
0610      */
0611     if (this && this->start == root->start) {
0612         tmp.start = (this == old) ? old->start : this->end + 1;
0613         this = this->sibling;
0614     }
0615     for(;;) {
0616         if (this)
0617             tmp.end = (this == old) ?  this->end : this->start - 1;
0618         else
0619             tmp.end = root->end;
0620 
0621         if (tmp.end < tmp.start)
0622             goto next;
0623 
0624         resource_clip(&tmp, constraint->min, constraint->max);
0625         arch_remove_reservations(&tmp);
0626 
0627         /* Check for overflow after ALIGN() */
0628         avail.start = ALIGN(tmp.start, constraint->align);
0629         avail.end = tmp.end;
0630         avail.flags = new->flags & ~IORESOURCE_UNSET;
0631         if (avail.start >= tmp.start) {
0632             alloc.flags = avail.flags;
0633             alloc.start = constraint->alignf(constraint->alignf_data, &avail,
0634                     size, constraint->align);
0635             alloc.end = alloc.start + size - 1;
0636             if (resource_contains(&avail, &alloc)) {
0637                 new->start = alloc.start;
0638                 new->end = alloc.end;
0639                 return 0;
0640             }
0641         }
0642 
0643 next:       if (!this || this->end == root->end)
0644             break;
0645 
0646         if (this != old)
0647             tmp.start = this->end + 1;
0648         this = this->sibling;
0649     }
0650     return -EBUSY;
0651 }
0652 
0653 /*
0654  * Find empty slot in the resource tree given range and alignment.
0655  */
0656 static int find_resource(struct resource *root, struct resource *new,
0657             resource_size_t size,
0658             struct resource_constraint  *constraint)
0659 {
0660     return  __find_resource(root, NULL, new, size, constraint);
0661 }
0662 
0663 /**
0664  * reallocate_resource - allocate a slot in the resource tree given range & alignment.
0665  *  The resource will be relocated if the new size cannot be reallocated in the
0666  *  current location.
0667  *
0668  * @root: root resource descriptor
0669  * @old:  resource descriptor desired by caller
0670  * @newsize: new size of the resource descriptor
0671  * @constraint: the size and alignment constraints to be met.
0672  */
0673 static int reallocate_resource(struct resource *root, struct resource *old,
0674             resource_size_t newsize,
0675             struct resource_constraint  *constraint)
0676 {
0677     int err=0;
0678     struct resource new = *old;
0679     struct resource *conflict;
0680 
0681     write_lock(&resource_lock);
0682 
0683     if ((err = __find_resource(root, old, &new, newsize, constraint)))
0684         goto out;
0685 
0686     if (resource_contains(&new, old)) {
0687         old->start = new.start;
0688         old->end = new.end;
0689         goto out;
0690     }
0691 
0692     if (old->child) {
0693         err = -EBUSY;
0694         goto out;
0695     }
0696 
0697     if (resource_contains(old, &new)) {
0698         old->start = new.start;
0699         old->end = new.end;
0700     } else {
0701         __release_resource(old, true);
0702         *old = new;
0703         conflict = __request_resource(root, old);
0704         BUG_ON(conflict);
0705     }
0706 out:
0707     write_unlock(&resource_lock);
0708     return err;
0709 }
0710 
0711 
0712 /**
0713  * allocate_resource - allocate empty slot in the resource tree given range & alignment.
0714  *  The resource will be reallocated with a new size if it was already allocated
0715  * @root: root resource descriptor
0716  * @new: resource descriptor desired by caller
0717  * @size: requested resource region size
0718  * @min: minimum boundary to allocate
0719  * @max: maximum boundary to allocate
0720  * @align: alignment requested, in bytes
0721  * @alignf: alignment function, optional, called if not NULL
0722  * @alignf_data: arbitrary data to pass to the @alignf function
0723  */
0724 int allocate_resource(struct resource *root, struct resource *new,
0725               resource_size_t size, resource_size_t min,
0726               resource_size_t max, resource_size_t align,
0727               resource_size_t (*alignf)(void *,
0728                         const struct resource *,
0729                         resource_size_t,
0730                         resource_size_t),
0731               void *alignf_data)
0732 {
0733     int err;
0734     struct resource_constraint constraint;
0735 
0736     if (!alignf)
0737         alignf = simple_align_resource;
0738 
0739     constraint.min = min;
0740     constraint.max = max;
0741     constraint.align = align;
0742     constraint.alignf = alignf;
0743     constraint.alignf_data = alignf_data;
0744 
0745     if ( new->parent ) {
0746         /* resource is already allocated, try reallocating with
0747            the new constraints */
0748         return reallocate_resource(root, new, size, &constraint);
0749     }
0750 
0751     write_lock(&resource_lock);
0752     err = find_resource(root, new, size, &constraint);
0753     if (err >= 0 && __request_resource(root, new))
0754         err = -EBUSY;
0755     write_unlock(&resource_lock);
0756     return err;
0757 }
0758 
0759 EXPORT_SYMBOL(allocate_resource);
0760 
0761 /**
0762  * lookup_resource - find an existing resource by a resource start address
0763  * @root: root resource descriptor
0764  * @start: resource start address
0765  *
0766  * Returns a pointer to the resource if found, NULL otherwise
0767  */
0768 struct resource *lookup_resource(struct resource *root, resource_size_t start)
0769 {
0770     struct resource *res;
0771 
0772     read_lock(&resource_lock);
0773     for (res = root->child; res; res = res->sibling) {
0774         if (res->start == start)
0775             break;
0776     }
0777     read_unlock(&resource_lock);
0778 
0779     return res;
0780 }
0781 
0782 /*
0783  * Insert a resource into the resource tree. If successful, return NULL,
0784  * otherwise return the conflicting resource (compare to __request_resource())
0785  */
0786 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
0787 {
0788     struct resource *first, *next;
0789 
0790     for (;; parent = first) {
0791         first = __request_resource(parent, new);
0792         if (!first)
0793             return first;
0794 
0795         if (first == parent)
0796             return first;
0797         if (WARN_ON(first == new))  /* duplicated insertion */
0798             return first;
0799 
0800         if ((first->start > new->start) || (first->end < new->end))
0801             break;
0802         if ((first->start == new->start) && (first->end == new->end))
0803             break;
0804     }
0805 
0806     for (next = first; ; next = next->sibling) {
0807         /* Partial overlap? Bad, and unfixable */
0808         if (next->start < new->start || next->end > new->end)
0809             return next;
0810         if (!next->sibling)
0811             break;
0812         if (next->sibling->start > new->end)
0813             break;
0814     }
0815 
0816     new->parent = parent;
0817     new->sibling = next->sibling;
0818     new->child = first;
0819 
0820     next->sibling = NULL;
0821     for (next = first; next; next = next->sibling)
0822         next->parent = new;
0823 
0824     if (parent->child == first) {
0825         parent->child = new;
0826     } else {
0827         next = parent->child;
0828         while (next->sibling != first)
0829             next = next->sibling;
0830         next->sibling = new;
0831     }
0832     return NULL;
0833 }
0834 
0835 /**
0836  * insert_resource_conflict - Inserts resource in the resource tree
0837  * @parent: parent of the new resource
0838  * @new: new resource to insert
0839  *
0840  * Returns 0 on success, conflict resource if the resource can't be inserted.
0841  *
0842  * This function is equivalent to request_resource_conflict when no conflict
0843  * happens. If a conflict happens, and the conflicting resources
0844  * entirely fit within the range of the new resource, then the new
0845  * resource is inserted and the conflicting resources become children of
0846  * the new resource.
0847  *
0848  * This function is intended for producers of resources, such as FW modules
0849  * and bus drivers.
0850  */
0851 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
0852 {
0853     struct resource *conflict;
0854 
0855     write_lock(&resource_lock);
0856     conflict = __insert_resource(parent, new);
0857     write_unlock(&resource_lock);
0858     return conflict;
0859 }
0860 
0861 /**
0862  * insert_resource - Inserts a resource in the resource tree
0863  * @parent: parent of the new resource
0864  * @new: new resource to insert
0865  *
0866  * Returns 0 on success, -EBUSY if the resource can't be inserted.
0867  *
0868  * This function is intended for producers of resources, such as FW modules
0869  * and bus drivers.
0870  */
0871 int insert_resource(struct resource *parent, struct resource *new)
0872 {
0873     struct resource *conflict;
0874 
0875     conflict = insert_resource_conflict(parent, new);
0876     return conflict ? -EBUSY : 0;
0877 }
0878 EXPORT_SYMBOL_GPL(insert_resource);
0879 
0880 /**
0881  * insert_resource_expand_to_fit - Insert a resource into the resource tree
0882  * @root: root resource descriptor
0883  * @new: new resource to insert
0884  *
0885  * Insert a resource into the resource tree, possibly expanding it in order
0886  * to make it encompass any conflicting resources.
0887  */
0888 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
0889 {
0890     if (new->parent)
0891         return;
0892 
0893     write_lock(&resource_lock);
0894     for (;;) {
0895         struct resource *conflict;
0896 
0897         conflict = __insert_resource(root, new);
0898         if (!conflict)
0899             break;
0900         if (conflict == root)
0901             break;
0902 
0903         /* Ok, expand resource to cover the conflict, then try again .. */
0904         if (conflict->start < new->start)
0905             new->start = conflict->start;
0906         if (conflict->end > new->end)
0907             new->end = conflict->end;
0908 
0909         printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
0910     }
0911     write_unlock(&resource_lock);
0912 }
0913 
0914 /**
0915  * remove_resource - Remove a resource in the resource tree
0916  * @old: resource to remove
0917  *
0918  * Returns 0 on success, -EINVAL if the resource is not valid.
0919  *
0920  * This function removes a resource previously inserted by insert_resource()
0921  * or insert_resource_conflict(), and moves the children (if any) up to
0922  * where they were before.  insert_resource() and insert_resource_conflict()
0923  * insert a new resource, and move any conflicting resources down to the
0924  * children of the new resource.
0925  *
0926  * insert_resource(), insert_resource_conflict() and remove_resource() are
0927  * intended for producers of resources, such as FW modules and bus drivers.
0928  */
0929 int remove_resource(struct resource *old)
0930 {
0931     int retval;
0932 
0933     write_lock(&resource_lock);
0934     retval = __release_resource(old, false);
0935     write_unlock(&resource_lock);
0936     return retval;
0937 }
0938 EXPORT_SYMBOL_GPL(remove_resource);
0939 
0940 static int __adjust_resource(struct resource *res, resource_size_t start,
0941                 resource_size_t size)
0942 {
0943     struct resource *tmp, *parent = res->parent;
0944     resource_size_t end = start + size - 1;
0945     int result = -EBUSY;
0946 
0947     if (!parent)
0948         goto skip;
0949 
0950     if ((start < parent->start) || (end > parent->end))
0951         goto out;
0952 
0953     if (res->sibling && (res->sibling->start <= end))
0954         goto out;
0955 
0956     tmp = parent->child;
0957     if (tmp != res) {
0958         while (tmp->sibling != res)
0959             tmp = tmp->sibling;
0960         if (start <= tmp->end)
0961             goto out;
0962     }
0963 
0964 skip:
0965     for (tmp = res->child; tmp; tmp = tmp->sibling)
0966         if ((tmp->start < start) || (tmp->end > end))
0967             goto out;
0968 
0969     res->start = start;
0970     res->end = end;
0971     result = 0;
0972 
0973  out:
0974     return result;
0975 }
0976 
0977 /**
0978  * adjust_resource - modify a resource's start and size
0979  * @res: resource to modify
0980  * @start: new start value
0981  * @size: new size
0982  *
0983  * Given an existing resource, change its start and size to match the
0984  * arguments.  Returns 0 on success, -EBUSY if it can't fit.
0985  * Existing children of the resource are assumed to be immutable.
0986  */
0987 int adjust_resource(struct resource *res, resource_size_t start,
0988             resource_size_t size)
0989 {
0990     int result;
0991 
0992     write_lock(&resource_lock);
0993     result = __adjust_resource(res, start, size);
0994     write_unlock(&resource_lock);
0995     return result;
0996 }
0997 EXPORT_SYMBOL(adjust_resource);
0998 
0999 static void __init __reserve_region_with_split(struct resource *root,
1000         resource_size_t start, resource_size_t end,
1001         const char *name)
1002 {
1003     struct resource *parent = root;
1004     struct resource *conflict;
1005     struct resource *res = alloc_resource(GFP_ATOMIC);
1006     struct resource *next_res = NULL;
1007 
1008     if (!res)
1009         return;
1010 
1011     res->name = name;
1012     res->start = start;
1013     res->end = end;
1014     res->flags = IORESOURCE_BUSY;
1015     res->desc = IORES_DESC_NONE;
1016 
1017     while (1) {
1018 
1019         conflict = __request_resource(parent, res);
1020         if (!conflict) {
1021             if (!next_res)
1022                 break;
1023             res = next_res;
1024             next_res = NULL;
1025             continue;
1026         }
1027 
1028         /* conflict covered whole area */
1029         if (conflict->start <= res->start &&
1030                 conflict->end >= res->end) {
1031             free_resource(res);
1032             WARN_ON(next_res);
1033             break;
1034         }
1035 
1036         /* failed, split and try again */
1037         if (conflict->start > res->start) {
1038             end = res->end;
1039             res->end = conflict->start - 1;
1040             if (conflict->end < end) {
1041                 next_res = alloc_resource(GFP_ATOMIC);
1042                 if (!next_res) {
1043                     free_resource(res);
1044                     break;
1045                 }
1046                 next_res->name = name;
1047                 next_res->start = conflict->end + 1;
1048                 next_res->end = end;
1049                 next_res->flags = IORESOURCE_BUSY;
1050                 next_res->desc = IORES_DESC_NONE;
1051             }
1052         } else {
1053             res->start = conflict->end + 1;
1054         }
1055     }
1056 
1057 }
1058 
1059 void __init reserve_region_with_split(struct resource *root,
1060         resource_size_t start, resource_size_t end,
1061         const char *name)
1062 {
1063     int abort = 0;
1064 
1065     write_lock(&resource_lock);
1066     if (root->start > start || root->end < end) {
1067         pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1068                (unsigned long long)start, (unsigned long long)end,
1069                root);
1070         if (start > root->end || end < root->start)
1071             abort = 1;
1072         else {
1073             if (end > root->end)
1074                 end = root->end;
1075             if (start < root->start)
1076                 start = root->start;
1077             pr_err("fixing request to [0x%llx-0x%llx]\n",
1078                    (unsigned long long)start,
1079                    (unsigned long long)end);
1080         }
1081         dump_stack();
1082     }
1083     if (!abort)
1084         __reserve_region_with_split(root, start, end, name);
1085     write_unlock(&resource_lock);
1086 }
1087 
1088 /**
1089  * resource_alignment - calculate resource's alignment
1090  * @res: resource pointer
1091  *
1092  * Returns alignment on success, 0 (invalid alignment) on failure.
1093  */
1094 resource_size_t resource_alignment(struct resource *res)
1095 {
1096     switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1097     case IORESOURCE_SIZEALIGN:
1098         return resource_size(res);
1099     case IORESOURCE_STARTALIGN:
1100         return res->start;
1101     default:
1102         return 0;
1103     }
1104 }
1105 
1106 /*
1107  * This is compatibility stuff for IO resources.
1108  *
1109  * Note how this, unlike the above, knows about
1110  * the IO flag meanings (busy etc).
1111  *
1112  * request_region creates a new busy region.
1113  *
1114  * release_region releases a matching busy region.
1115  */
1116 
1117 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1118 
1119 /**
1120  * __request_region - create a new busy resource region
1121  * @parent: parent resource descriptor
1122  * @start: resource start address
1123  * @n: resource region size
1124  * @name: reserving caller's ID string
1125  * @flags: IO resource flags
1126  */
1127 struct resource * __request_region(struct resource *parent,
1128                    resource_size_t start, resource_size_t n,
1129                    const char *name, int flags)
1130 {
1131     DECLARE_WAITQUEUE(wait, current);
1132     struct resource *res = alloc_resource(GFP_KERNEL);
1133 
1134     if (!res)
1135         return NULL;
1136 
1137     res->name = name;
1138     res->start = start;
1139     res->end = start + n - 1;
1140 
1141     write_lock(&resource_lock);
1142 
1143     for (;;) {
1144         struct resource *conflict;
1145 
1146         res->flags = resource_type(parent) | resource_ext_type(parent);
1147         res->flags |= IORESOURCE_BUSY | flags;
1148         res->desc = parent->desc;
1149 
1150         conflict = __request_resource(parent, res);
1151         if (!conflict)
1152             break;
1153         if (conflict != parent) {
1154             if (!(conflict->flags & IORESOURCE_BUSY)) {
1155                 parent = conflict;
1156                 continue;
1157             }
1158         }
1159         if (conflict->flags & flags & IORESOURCE_MUXED) {
1160             add_wait_queue(&muxed_resource_wait, &wait);
1161             write_unlock(&resource_lock);
1162             set_current_state(TASK_UNINTERRUPTIBLE);
1163             schedule();
1164             remove_wait_queue(&muxed_resource_wait, &wait);
1165             write_lock(&resource_lock);
1166             continue;
1167         }
1168         /* Uhhuh, that didn't work out.. */
1169         free_resource(res);
1170         res = NULL;
1171         break;
1172     }
1173     write_unlock(&resource_lock);
1174     return res;
1175 }
1176 EXPORT_SYMBOL(__request_region);
1177 
1178 /**
1179  * __release_region - release a previously reserved resource region
1180  * @parent: parent resource descriptor
1181  * @start: resource start address
1182  * @n: resource region size
1183  *
1184  * The described resource region must match a currently busy region.
1185  */
1186 void __release_region(struct resource *parent, resource_size_t start,
1187             resource_size_t n)
1188 {
1189     struct resource **p;
1190     resource_size_t end;
1191 
1192     p = &parent->child;
1193     end = start + n - 1;
1194 
1195     write_lock(&resource_lock);
1196 
1197     for (;;) {
1198         struct resource *res = *p;
1199 
1200         if (!res)
1201             break;
1202         if (res->start <= start && res->end >= end) {
1203             if (!(res->flags & IORESOURCE_BUSY)) {
1204                 p = &res->child;
1205                 continue;
1206             }
1207             if (res->start != start || res->end != end)
1208                 break;
1209             *p = res->sibling;
1210             write_unlock(&resource_lock);
1211             if (res->flags & IORESOURCE_MUXED)
1212                 wake_up(&muxed_resource_wait);
1213             free_resource(res);
1214             return;
1215         }
1216         p = &res->sibling;
1217     }
1218 
1219     write_unlock(&resource_lock);
1220 
1221     printk(KERN_WARNING "Trying to free nonexistent resource "
1222         "<%016llx-%016llx>\n", (unsigned long long)start,
1223         (unsigned long long)end);
1224 }
1225 EXPORT_SYMBOL(__release_region);
1226 
1227 #ifdef CONFIG_MEMORY_HOTREMOVE
1228 /**
1229  * release_mem_region_adjustable - release a previously reserved memory region
1230  * @parent: parent resource descriptor
1231  * @start: resource start address
1232  * @size: resource region size
1233  *
1234  * This interface is intended for memory hot-delete.  The requested region
1235  * is released from a currently busy memory resource.  The requested region
1236  * must either match exactly or fit into a single busy resource entry.  In
1237  * the latter case, the remaining resource is adjusted accordingly.
1238  * Existing children of the busy memory resource must be immutable in the
1239  * request.
1240  *
1241  * Note:
1242  * - Additional release conditions, such as overlapping region, can be
1243  *   supported after they are confirmed as valid cases.
1244  * - When a busy memory resource gets split into two entries, the code
1245  *   assumes that all children remain in the lower address entry for
1246  *   simplicity.  Enhance this logic when necessary.
1247  */
1248 int release_mem_region_adjustable(struct resource *parent,
1249             resource_size_t start, resource_size_t size)
1250 {
1251     struct resource **p;
1252     struct resource *res;
1253     struct resource *new_res;
1254     resource_size_t end;
1255     int ret = -EINVAL;
1256 
1257     end = start + size - 1;
1258     if ((start < parent->start) || (end > parent->end))
1259         return ret;
1260 
1261     /* The alloc_resource() result gets checked later */
1262     new_res = alloc_resource(GFP_KERNEL);
1263 
1264     p = &parent->child;
1265     write_lock(&resource_lock);
1266 
1267     while ((res = *p)) {
1268         if (res->start >= end)
1269             break;
1270 
1271         /* look for the next resource if it does not fit into */
1272         if (res->start > start || res->end < end) {
1273             p = &res->sibling;
1274             continue;
1275         }
1276 
1277         if (!(res->flags & IORESOURCE_MEM))
1278             break;
1279 
1280         if (!(res->flags & IORESOURCE_BUSY)) {
1281             p = &res->child;
1282             continue;
1283         }
1284 
1285         /* found the target resource; let's adjust accordingly */
1286         if (res->start == start && res->end == end) {
1287             /* free the whole entry */
1288             *p = res->sibling;
1289             free_resource(res);
1290             ret = 0;
1291         } else if (res->start == start && res->end != end) {
1292             /* adjust the start */
1293             ret = __adjust_resource(res, end + 1,
1294                         res->end - end);
1295         } else if (res->start != start && res->end == end) {
1296             /* adjust the end */
1297             ret = __adjust_resource(res, res->start,
1298                         start - res->start);
1299         } else {
1300             /* split into two entries */
1301             if (!new_res) {
1302                 ret = -ENOMEM;
1303                 break;
1304             }
1305             new_res->name = res->name;
1306             new_res->start = end + 1;
1307             new_res->end = res->end;
1308             new_res->flags = res->flags;
1309             new_res->desc = res->desc;
1310             new_res->parent = res->parent;
1311             new_res->sibling = res->sibling;
1312             new_res->child = NULL;
1313 
1314             ret = __adjust_resource(res, res->start,
1315                         start - res->start);
1316             if (ret)
1317                 break;
1318             res->sibling = new_res;
1319             new_res = NULL;
1320         }
1321 
1322         break;
1323     }
1324 
1325     write_unlock(&resource_lock);
1326     free_resource(new_res);
1327     return ret;
1328 }
1329 #endif  /* CONFIG_MEMORY_HOTREMOVE */
1330 
1331 /*
1332  * Managed region resource
1333  */
1334 static void devm_resource_release(struct device *dev, void *ptr)
1335 {
1336     struct resource **r = ptr;
1337 
1338     release_resource(*r);
1339 }
1340 
1341 /**
1342  * devm_request_resource() - request and reserve an I/O or memory resource
1343  * @dev: device for which to request the resource
1344  * @root: root of the resource tree from which to request the resource
1345  * @new: descriptor of the resource to request
1346  *
1347  * This is a device-managed version of request_resource(). There is usually
1348  * no need to release resources requested by this function explicitly since
1349  * that will be taken care of when the device is unbound from its driver.
1350  * If for some reason the resource needs to be released explicitly, because
1351  * of ordering issues for example, drivers must call devm_release_resource()
1352  * rather than the regular release_resource().
1353  *
1354  * When a conflict is detected between any existing resources and the newly
1355  * requested resource, an error message will be printed.
1356  *
1357  * Returns 0 on success or a negative error code on failure.
1358  */
1359 int devm_request_resource(struct device *dev, struct resource *root,
1360               struct resource *new)
1361 {
1362     struct resource *conflict, **ptr;
1363 
1364     ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1365     if (!ptr)
1366         return -ENOMEM;
1367 
1368     *ptr = new;
1369 
1370     conflict = request_resource_conflict(root, new);
1371     if (conflict) {
1372         dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1373             new, conflict->name, conflict);
1374         devres_free(ptr);
1375         return -EBUSY;
1376     }
1377 
1378     devres_add(dev, ptr);
1379     return 0;
1380 }
1381 EXPORT_SYMBOL(devm_request_resource);
1382 
1383 static int devm_resource_match(struct device *dev, void *res, void *data)
1384 {
1385     struct resource **ptr = res;
1386 
1387     return *ptr == data;
1388 }
1389 
1390 /**
1391  * devm_release_resource() - release a previously requested resource
1392  * @dev: device for which to release the resource
1393  * @new: descriptor of the resource to release
1394  *
1395  * Releases a resource previously requested using devm_request_resource().
1396  */
1397 void devm_release_resource(struct device *dev, struct resource *new)
1398 {
1399     WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1400                    new));
1401 }
1402 EXPORT_SYMBOL(devm_release_resource);
1403 
1404 struct region_devres {
1405     struct resource *parent;
1406     resource_size_t start;
1407     resource_size_t n;
1408 };
1409 
1410 static void devm_region_release(struct device *dev, void *res)
1411 {
1412     struct region_devres *this = res;
1413 
1414     __release_region(this->parent, this->start, this->n);
1415 }
1416 
1417 static int devm_region_match(struct device *dev, void *res, void *match_data)
1418 {
1419     struct region_devres *this = res, *match = match_data;
1420 
1421     return this->parent == match->parent &&
1422         this->start == match->start && this->n == match->n;
1423 }
1424 
1425 struct resource * __devm_request_region(struct device *dev,
1426                 struct resource *parent, resource_size_t start,
1427                 resource_size_t n, const char *name)
1428 {
1429     struct region_devres *dr = NULL;
1430     struct resource *res;
1431 
1432     dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1433               GFP_KERNEL);
1434     if (!dr)
1435         return NULL;
1436 
1437     dr->parent = parent;
1438     dr->start = start;
1439     dr->n = n;
1440 
1441     res = __request_region(parent, start, n, name, 0);
1442     if (res)
1443         devres_add(dev, dr);
1444     else
1445         devres_free(dr);
1446 
1447     return res;
1448 }
1449 EXPORT_SYMBOL(__devm_request_region);
1450 
1451 void __devm_release_region(struct device *dev, struct resource *parent,
1452                resource_size_t start, resource_size_t n)
1453 {
1454     struct region_devres match_data = { parent, start, n };
1455 
1456     __release_region(parent, start, n);
1457     WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1458                    &match_data));
1459 }
1460 EXPORT_SYMBOL(__devm_release_region);
1461 
1462 /*
1463  * Called from init/main.c to reserve IO ports.
1464  */
1465 #define MAXRESERVE 4
1466 static int __init reserve_setup(char *str)
1467 {
1468     static int reserved;
1469     static struct resource reserve[MAXRESERVE];
1470 
1471     for (;;) {
1472         unsigned int io_start, io_num;
1473         int x = reserved;
1474 
1475         if (get_option (&str, &io_start) != 2)
1476             break;
1477         if (get_option (&str, &io_num)   == 0)
1478             break;
1479         if (x < MAXRESERVE) {
1480             struct resource *res = reserve + x;
1481             res->name = "reserved";
1482             res->start = io_start;
1483             res->end = io_start + io_num - 1;
1484             res->flags = IORESOURCE_BUSY;
1485             res->desc = IORES_DESC_NONE;
1486             res->child = NULL;
1487             if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
1488                 reserved = x+1;
1489         }
1490     }
1491     return 1;
1492 }
1493 
1494 __setup("reserve=", reserve_setup);
1495 
1496 /*
1497  * Check if the requested addr and size spans more than any slot in the
1498  * iomem resource tree.
1499  */
1500 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1501 {
1502     struct resource *p = &iomem_resource;
1503     int err = 0;
1504     loff_t l;
1505 
1506     read_lock(&resource_lock);
1507     for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1508         /*
1509          * We can probably skip the resources without
1510          * IORESOURCE_IO attribute?
1511          */
1512         if (p->start >= addr + size)
1513             continue;
1514         if (p->end < addr)
1515             continue;
1516         if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1517             PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1518             continue;
1519         /*
1520          * if a resource is "BUSY", it's not a hardware resource
1521          * but a driver mapping of such a resource; we don't want
1522          * to warn for those; some drivers legitimately map only
1523          * partial hardware resources. (example: vesafb)
1524          */
1525         if (p->flags & IORESOURCE_BUSY)
1526             continue;
1527 
1528         printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1529                (unsigned long long)addr,
1530                (unsigned long long)(addr + size - 1),
1531                p->name, p);
1532         err = -1;
1533         break;
1534     }
1535     read_unlock(&resource_lock);
1536 
1537     return err;
1538 }
1539 
1540 #ifdef CONFIG_STRICT_DEVMEM
1541 static int strict_iomem_checks = 1;
1542 #else
1543 static int strict_iomem_checks;
1544 #endif
1545 
1546 /*
1547  * check if an address is reserved in the iomem resource tree
1548  * returns 1 if reserved, 0 if not reserved.
1549  */
1550 int iomem_is_exclusive(u64 addr)
1551 {
1552     struct resource *p = &iomem_resource;
1553     int err = 0;
1554     loff_t l;
1555     int size = PAGE_SIZE;
1556 
1557     if (!strict_iomem_checks)
1558         return 0;
1559 
1560     addr = addr & PAGE_MASK;
1561 
1562     read_lock(&resource_lock);
1563     for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1564         /*
1565          * We can probably skip the resources without
1566          * IORESOURCE_IO attribute?
1567          */
1568         if (p->start >= addr + size)
1569             break;
1570         if (p->end < addr)
1571             continue;
1572         /*
1573          * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1574          * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1575          * resource is busy.
1576          */
1577         if ((p->flags & IORESOURCE_BUSY) == 0)
1578             continue;
1579         if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1580                 || p->flags & IORESOURCE_EXCLUSIVE) {
1581             err = 1;
1582             break;
1583         }
1584     }
1585     read_unlock(&resource_lock);
1586 
1587     return err;
1588 }
1589 
1590 struct resource_entry *resource_list_create_entry(struct resource *res,
1591                           size_t extra_size)
1592 {
1593     struct resource_entry *entry;
1594 
1595     entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1596     if (entry) {
1597         INIT_LIST_HEAD(&entry->node);
1598         entry->res = res ? res : &entry->__res;
1599     }
1600 
1601     return entry;
1602 }
1603 EXPORT_SYMBOL(resource_list_create_entry);
1604 
1605 void resource_list_free(struct list_head *head)
1606 {
1607     struct resource_entry *entry, *tmp;
1608 
1609     list_for_each_entry_safe(entry, tmp, head, node)
1610         resource_list_destroy_entry(entry);
1611 }
1612 EXPORT_SYMBOL(resource_list_free);
1613 
1614 static int __init strict_iomem(char *str)
1615 {
1616     if (strstr(str, "relaxed"))
1617         strict_iomem_checks = 0;
1618     if (strstr(str, "strict"))
1619         strict_iomem_checks = 1;
1620     return 1;
1621 }
1622 
1623 __setup("iomem=", strict_iomem);