blob: 4aef8867fd4ba3105452046e3d1ada3ff45bdc4d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/resource.c
3 *
4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
6 *
7 * Arbitrary resource management.
8 */
9
Octavian Purdila65fed8f2012-07-30 14:42:58 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Paul Gortmaker9984de12011-05-23 14:51:41 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/ioport.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/fs.h>
19#include <linux/proc_fs.h>
Alan Cox8b6d0432010-03-29 19:38:00 +020020#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/seq_file.h>
Tejun Heo9ac78492007-01-20 16:00:26 +090022#include <linux/device.h>
Suresh Siddhad68612b2008-10-28 11:45:42 -070023#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/io.h>
25
26
27struct resource ioport_resource = {
28 .name = "PCI IO",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070029 .start = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .end = IO_SPACE_LIMIT,
31 .flags = IORESOURCE_IO,
32};
Linus Torvalds1da177e2005-04-16 15:20:36 -070033EXPORT_SYMBOL(ioport_resource);
34
35struct resource iomem_resource = {
36 .name = "PCI mem",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070037 .start = 0,
38 .end = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 .flags = IORESOURCE_MEM,
40};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041EXPORT_SYMBOL(iomem_resource);
42
Ram Pai23c570a2011-07-05 23:44:30 -070043/* constraints to be met while allocating resources */
44struct resource_constraint {
45 resource_size_t min, max, align;
46 resource_size_t (*alignf)(void *, const struct resource *,
47 resource_size_t, resource_size_t);
48 void *alignf_data;
49};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static DEFINE_RWLOCK(resource_lock);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static void *r_next(struct seq_file *m, void *v, loff_t *pos)
54{
55 struct resource *p = v;
56 (*pos)++;
57 if (p->child)
58 return p->child;
59 while (!p->sibling && p->parent)
60 p = p->parent;
61 return p->sibling;
62}
63
Ingo Molnar13eb8372008-09-26 10:10:12 +020064#ifdef CONFIG_PROC_FS
65
66enum { MAX_IORES_LEVEL = 5 };
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void *r_start(struct seq_file *m, loff_t *pos)
69 __acquires(resource_lock)
70{
71 struct resource *p = m->private;
72 loff_t l = 0;
73 read_lock(&resource_lock);
74 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
75 ;
76 return p;
77}
78
79static void r_stop(struct seq_file *m, void *v)
80 __releases(resource_lock)
81{
82 read_unlock(&resource_lock);
83}
84
85static int r_show(struct seq_file *m, void *v)
86{
87 struct resource *root = m->private;
88 struct resource *r = v, *p;
89 int width = root->end < 0x10000 ? 4 : 8;
90 int depth;
91
92 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
93 if (p->parent == root)
94 break;
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -070095 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 depth * 2, "",
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -070097 width, (unsigned long long) r->start,
98 width, (unsigned long long) r->end,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 r->name ? r->name : "<BAD>");
100 return 0;
101}
102
Helge Deller15ad7cd2006-12-06 20:40:36 -0800103static const struct seq_operations resource_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .start = r_start,
105 .next = r_next,
106 .stop = r_stop,
107 .show = r_show,
108};
109
110static int ioports_open(struct inode *inode, struct file *file)
111{
112 int res = seq_open(file, &resource_op);
113 if (!res) {
114 struct seq_file *m = file->private_data;
115 m->private = &ioport_resource;
116 }
117 return res;
118}
119
120static int iomem_open(struct inode *inode, struct file *file)
121{
122 int res = seq_open(file, &resource_op);
123 if (!res) {
124 struct seq_file *m = file->private_data;
125 m->private = &iomem_resource;
126 }
127 return res;
128}
129
Helge Deller15ad7cd2006-12-06 20:40:36 -0800130static const struct file_operations proc_ioports_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .open = ioports_open,
132 .read = seq_read,
133 .llseek = seq_lseek,
134 .release = seq_release,
135};
136
Helge Deller15ad7cd2006-12-06 20:40:36 -0800137static const struct file_operations proc_iomem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .open = iomem_open,
139 .read = seq_read,
140 .llseek = seq_lseek,
141 .release = seq_release,
142};
143
144static int __init ioresources_init(void)
145{
Denis V. Lunevc33fff02008-04-29 01:02:31 -0700146 proc_create("ioports", 0, NULL, &proc_ioports_operations);
147 proc_create("iomem", 0, NULL, &proc_iomem_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return 0;
149}
150__initcall(ioresources_init);
151
152#endif /* CONFIG_PROC_FS */
153
154/* Return the conflict entry if you can't request it */
155static struct resource * __request_resource(struct resource *root, struct resource *new)
156{
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700157 resource_size_t start = new->start;
158 resource_size_t end = new->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct resource *tmp, **p;
160
161 if (end < start)
162 return root;
163 if (start < root->start)
164 return root;
165 if (end > root->end)
166 return root;
167 p = &root->child;
168 for (;;) {
169 tmp = *p;
170 if (!tmp || tmp->start > end) {
171 new->sibling = tmp;
172 *p = new;
173 new->parent = root;
174 return NULL;
175 }
176 p = &tmp->sibling;
177 if (tmp->end < start)
178 continue;
179 return tmp;
180 }
181}
182
183static int __release_resource(struct resource *old)
184{
185 struct resource *tmp, **p;
186
187 p = &old->parent->child;
188 for (;;) {
189 tmp = *p;
190 if (!tmp)
191 break;
192 if (tmp == old) {
193 *p = tmp->sibling;
194 old->parent = NULL;
195 return 0;
196 }
197 p = &tmp->sibling;
198 }
199 return -EINVAL;
200}
201
Yinghai Lu5eeec0e2009-12-22 15:02:22 -0800202static void __release_child_resources(struct resource *r)
203{
204 struct resource *tmp, *p;
205 resource_size_t size;
206
207 p = r->child;
208 r->child = NULL;
209 while (p) {
210 tmp = p;
211 p = p->sibling;
212
213 tmp->parent = NULL;
214 tmp->sibling = NULL;
215 __release_child_resources(tmp);
216
217 printk(KERN_DEBUG "release child resource %pR\n", tmp);
218 /* need to restore size, and keep flags */
219 size = resource_size(tmp);
220 tmp->start = 0;
221 tmp->end = size - 1;
222 }
223}
224
225void release_child_resources(struct resource *r)
226{
227 write_lock(&resource_lock);
228 __release_child_resources(r);
229 write_unlock(&resource_lock);
230}
231
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700232/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700233 * request_resource_conflict - request and reserve an I/O or memory resource
234 * @root: root resource descriptor
235 * @new: resource descriptor desired by caller
236 *
237 * Returns 0 for success, conflict resource on error.
238 */
239struct resource *request_resource_conflict(struct resource *root, struct resource *new)
240{
241 struct resource *conflict;
242
243 write_lock(&resource_lock);
244 conflict = __request_resource(root, new);
245 write_unlock(&resource_lock);
246 return conflict;
247}
248
249/**
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700250 * request_resource - request and reserve an I/O or memory resource
251 * @root: root resource descriptor
252 * @new: resource descriptor desired by caller
253 *
254 * Returns 0 for success, negative error code on error.
255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256int request_resource(struct resource *root, struct resource *new)
257{
258 struct resource *conflict;
259
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700260 conflict = request_resource_conflict(root, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return conflict ? -EBUSY : 0;
262}
263
264EXPORT_SYMBOL(request_resource);
265
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700266/**
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700267 * release_resource - release a previously reserved resource
268 * @old: resource pointer
269 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270int release_resource(struct resource *old)
271{
272 int retval;
273
274 write_lock(&resource_lock);
275 retval = __release_resource(old);
276 write_unlock(&resource_lock);
277 return retval;
278}
279
280EXPORT_SYMBOL(release_resource);
281
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700282#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700283/*
284 * Finds the lowest memory reosurce exists within [res->start.res->end)
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700285 * the caller must specify res->start, res->end, res->flags and "name".
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700286 * If found, returns 0, res is overwritten, if not found, returns -1.
287 */
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700288static int find_next_system_ram(struct resource *res, char *name)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700289{
290 resource_size_t start, end;
291 struct resource *p;
292
293 BUG_ON(!res);
294
295 start = res->start;
296 end = res->end;
KAMEZAWA Hiroyuki58c1b5b2006-08-05 12:15:01 -0700297 BUG_ON(start >= end);
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700298
299 read_lock(&resource_lock);
300 for (p = iomem_resource.child; p ; p = p->sibling) {
301 /* system ram is just marked as IORESOURCE_MEM */
302 if (p->flags != res->flags)
303 continue;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700304 if (name && strcmp(p->name, name))
305 continue;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700306 if (p->start > end) {
307 p = NULL;
308 break;
309 }
KAMEZAWA Hiroyuki58c1b5b2006-08-05 12:15:01 -0700310 if ((p->end >= start) && (p->start < end))
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700311 break;
312 }
313 read_unlock(&resource_lock);
314 if (!p)
315 return -1;
316 /* copy data */
KAMEZAWA Hiroyuki0f04ab52006-08-05 12:14:59 -0700317 if (res->start < p->start)
318 res->start = p->start;
319 if (res->end > p->end)
320 res->end = p->end;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700321 return 0;
322}
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700323
324/*
325 * This function calls callback against all memory range of "System RAM"
326 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
327 * Now, this function is only for "System RAM".
328 */
329int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
330 void *arg, int (*func)(unsigned long, unsigned long, void *))
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700331{
332 struct resource res;
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800333 unsigned long pfn, end_pfn;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700334 u64 orig_end;
335 int ret = -1;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700336
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700337 res.start = (u64) start_pfn << PAGE_SHIFT;
338 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -0800339 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700340 orig_end = res.end;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700341 while ((res.start < res.end) &&
342 (find_next_system_ram(&res, "System RAM") >= 0)) {
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800343 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
344 end_pfn = (res.end + 1) >> PAGE_SHIFT;
345 if (end_pfn > pfn)
H. Peter Anvinf4149662010-03-02 11:21:09 -0800346 ret = (*func)(pfn, end_pfn - pfn, arg);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700347 if (ret)
348 break;
349 res.start = res.end + 1;
350 res.end = orig_end;
351 }
352 return ret;
353}
354
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700355#endif
356
Wu Fengguang61ef2482010-01-22 16:16:19 +0800357static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
358{
359 return 1;
360}
361/*
362 * This generic page_is_ram() returns true if specified address is
363 * registered as "System RAM" in iomem_resource list.
364 */
Andrew Mortone5273002010-01-26 16:31:19 -0800365int __weak page_is_ram(unsigned long pfn)
Wu Fengguang61ef2482010-01-22 16:16:19 +0800366{
367 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
368}
369
Bjorn Helgaasfcb11912010-12-16 10:38:46 -0700370void __weak arch_remove_reservations(struct resource *avail)
371{
372}
373
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600374static resource_size_t simple_align_resource(void *data,
375 const struct resource *avail,
376 resource_size_t size,
377 resource_size_t align)
378{
379 return avail->start;
380}
381
Bjorn Helgaas5d6b1fa2010-10-26 15:41:18 -0600382static void resource_clip(struct resource *res, resource_size_t min,
383 resource_size_t max)
384{
385 if (res->start < min)
386 res->start = min;
387 if (res->end > max)
388 res->end = max;
389}
390
Bjorn Helgaas6909ba12010-10-26 15:41:23 -0600391static bool resource_contains(struct resource *res1, struct resource *res2)
392{
393 return res1->start <= res2->start && res1->end >= res2->end;
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
Ram Pai23c570a2011-07-05 23:44:30 -0700397 * Find empty slot in the resource tree with the given range and
398 * alignment constraints
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Ram Pai23c570a2011-07-05 23:44:30 -0700400static int __find_resource(struct resource *root, struct resource *old,
401 struct resource *new,
402 resource_size_t size,
403 struct resource_constraint *constraint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct resource *this = root->child;
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600406 struct resource tmp = *new, avail, alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Bjorn Helgaasfcb11912010-12-16 10:38:46 -0700408 tmp.flags = new->flags;
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100409 tmp.start = root->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
Bjorn Helgaasc0f5ac52010-12-16 10:38:41 -0700411 * Skip past an allocated resource that starts at 0, since the assignment
412 * of this->start - 1 to tmp->end below would cause an underflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Ram Pai23c570a2011-07-05 23:44:30 -0700414 if (this && this->start == root->start) {
415 tmp.start = (this == old) ? old->start : this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 this = this->sibling;
417 }
Bjorn Helgaasc0f5ac52010-12-16 10:38:41 -0700418 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (this)
Ram Pai23c570a2011-07-05 23:44:30 -0700420 tmp.end = (this == old) ? this->end : this->start - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 else
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100422 tmp.end = root->end;
Bjorn Helgaas5d6b1fa2010-10-26 15:41:18 -0600423
Ram Pai47ea91b2011-09-22 15:48:58 +0800424 if (tmp.end < tmp.start)
425 goto next;
426
Ram Pai23c570a2011-07-05 23:44:30 -0700427 resource_clip(&tmp, constraint->min, constraint->max);
Bjorn Helgaasfcb11912010-12-16 10:38:46 -0700428 arch_remove_reservations(&tmp);
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600429
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600430 /* Check for overflow after ALIGN() */
431 avail = *new;
Ram Pai23c570a2011-07-05 23:44:30 -0700432 avail.start = ALIGN(tmp.start, constraint->align);
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600433 avail.end = tmp.end;
434 if (avail.start >= tmp.start) {
Ram Pai23c570a2011-07-05 23:44:30 -0700435 alloc.start = constraint->alignf(constraint->alignf_data, &avail,
436 size, constraint->align);
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600437 alloc.end = alloc.start + size - 1;
438 if (resource_contains(&avail, &alloc)) {
439 new->start = alloc.start;
440 new->end = alloc.end;
441 return 0;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Ram Pai47ea91b2011-09-22 15:48:58 +0800444
445next: if (!this || this->end == root->end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
Ram Pai47ea91b2011-09-22 15:48:58 +0800447
Ram Pai23c570a2011-07-05 23:44:30 -0700448 if (this != old)
449 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 this = this->sibling;
451 }
452 return -EBUSY;
453}
454
Ram Pai23c570a2011-07-05 23:44:30 -0700455/*
456 * Find empty slot in the resource tree given range and alignment.
457 */
458static int find_resource(struct resource *root, struct resource *new,
459 resource_size_t size,
460 struct resource_constraint *constraint)
461{
462 return __find_resource(root, NULL, new, size, constraint);
463}
464
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700465/**
Ram Pai23c570a2011-07-05 23:44:30 -0700466 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
467 * The resource will be relocated if the new size cannot be reallocated in the
468 * current location.
469 *
470 * @root: root resource descriptor
471 * @old: resource descriptor desired by caller
472 * @newsize: new size of the resource descriptor
473 * @constraint: the size and alignment constraints to be met.
474 */
475int reallocate_resource(struct resource *root, struct resource *old,
476 resource_size_t newsize,
477 struct resource_constraint *constraint)
478{
479 int err=0;
480 struct resource new = *old;
481 struct resource *conflict;
482
483 write_lock(&resource_lock);
484
485 if ((err = __find_resource(root, old, &new, newsize, constraint)))
486 goto out;
487
488 if (resource_contains(&new, old)) {
489 old->start = new.start;
490 old->end = new.end;
491 goto out;
492 }
493
494 if (old->child) {
495 err = -EBUSY;
496 goto out;
497 }
498
499 if (resource_contains(old, &new)) {
500 old->start = new.start;
501 old->end = new.end;
502 } else {
503 __release_resource(old);
504 *old = new;
505 conflict = __request_resource(root, old);
506 BUG_ON(conflict);
507 }
508out:
509 write_unlock(&resource_lock);
510 return err;
511}
512
513
514/**
515 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
516 * The resource will be reallocated with a new size if it was already allocated
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700517 * @root: root resource descriptor
518 * @new: resource descriptor desired by caller
519 * @size: requested resource region size
Wei Yangee5e5682012-05-31 16:26:05 -0700520 * @min: minimum boundary to allocate
521 * @max: maximum boundary to allocate
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700522 * @align: alignment requested, in bytes
523 * @alignf: alignment function, optional, called if not NULL
524 * @alignf_data: arbitrary data to pass to the @alignf function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
526int allocate_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700527 resource_size_t size, resource_size_t min,
528 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100529 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100530 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100531 resource_size_t,
532 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 void *alignf_data)
534{
535 int err;
Ram Pai23c570a2011-07-05 23:44:30 -0700536 struct resource_constraint constraint;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600538 if (!alignf)
539 alignf = simple_align_resource;
540
Ram Pai23c570a2011-07-05 23:44:30 -0700541 constraint.min = min;
542 constraint.max = max;
543 constraint.align = align;
544 constraint.alignf = alignf;
545 constraint.alignf_data = alignf_data;
546
547 if ( new->parent ) {
548 /* resource is already allocated, try reallocating with
549 the new constraints */
550 return reallocate_resource(root, new, size, &constraint);
551 }
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 write_lock(&resource_lock);
Ram Pai23c570a2011-07-05 23:44:30 -0700554 err = find_resource(root, new, size, &constraint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (err >= 0 && __request_resource(root, new))
556 err = -EBUSY;
557 write_unlock(&resource_lock);
558 return err;
559}
560
561EXPORT_SYMBOL(allocate_resource);
562
Geert Uytterhoeven1c388912011-05-07 20:53:16 +0200563/**
564 * lookup_resource - find an existing resource by a resource start address
565 * @root: root resource descriptor
566 * @start: resource start address
567 *
568 * Returns a pointer to the resource if found, NULL otherwise
569 */
570struct resource *lookup_resource(struct resource *root, resource_size_t start)
571{
572 struct resource *res;
573
574 read_lock(&resource_lock);
575 for (res = root->child; res; res = res->sibling) {
576 if (res->start == start)
577 break;
578 }
579 read_unlock(&resource_lock);
580
581 return res;
582}
583
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700584/*
585 * Insert a resource into the resource tree. If successful, return NULL,
586 * otherwise return the conflicting resource (compare to __request_resource())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700588static struct resource * __insert_resource(struct resource *parent, struct resource *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct resource *first, *next;
591
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700592 for (;; parent = first) {
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700593 first = __request_resource(parent, new);
594 if (!first)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700595 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700597 if (first == parent)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700598 return first;
Huang Shijie5de1cb22010-10-27 15:34:52 -0700599 if (WARN_ON(first == new)) /* duplicated insertion */
600 return first;
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700601
602 if ((first->start > new->start) || (first->end < new->end))
603 break;
604 if ((first->start == new->start) && (first->end == new->end))
605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 for (next = first; ; next = next->sibling) {
609 /* Partial overlap? Bad, and unfixable */
610 if (next->start < new->start || next->end > new->end)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700611 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!next->sibling)
613 break;
614 if (next->sibling->start > new->end)
615 break;
616 }
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 new->parent = parent;
619 new->sibling = next->sibling;
620 new->child = first;
621
622 next->sibling = NULL;
623 for (next = first; next; next = next->sibling)
624 next->parent = new;
625
626 if (parent->child == first) {
627 parent->child = new;
628 } else {
629 next = parent->child;
630 while (next->sibling != first)
631 next = next->sibling;
632 next->sibling = new;
633 }
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700634 return NULL;
635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700637/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700638 * insert_resource_conflict - Inserts resource in the resource tree
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700639 * @parent: parent of the new resource
640 * @new: new resource to insert
641 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700642 * Returns 0 on success, conflict resource if the resource can't be inserted.
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700643 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700644 * This function is equivalent to request_resource_conflict when no conflict
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700645 * happens. If a conflict happens, and the conflicting resources
646 * entirely fit within the range of the new resource, then the new
647 * resource is inserted and the conflicting resources become children of
648 * the new resource.
649 */
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700650struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700651{
652 struct resource *conflict;
653
654 write_lock(&resource_lock);
655 conflict = __insert_resource(parent, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 write_unlock(&resource_lock);
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700657 return conflict;
658}
659
660/**
661 * insert_resource - Inserts a resource in the resource tree
662 * @parent: parent of the new resource
663 * @new: new resource to insert
664 *
665 * Returns 0 on success, -EBUSY if the resource can't be inserted.
666 */
667int insert_resource(struct resource *parent, struct resource *new)
668{
669 struct resource *conflict;
670
671 conflict = insert_resource_conflict(parent, new);
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700672 return conflict ? -EBUSY : 0;
673}
674
675/**
676 * insert_resource_expand_to_fit - Insert a resource into the resource tree
Randy Dunlap6781f4a2008-08-31 20:31:55 -0700677 * @root: root resource descriptor
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700678 * @new: new resource to insert
679 *
680 * Insert a resource into the resource tree, possibly expanding it in order
681 * to make it encompass any conflicting resources.
682 */
683void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
684{
685 if (new->parent)
686 return;
687
688 write_lock(&resource_lock);
689 for (;;) {
690 struct resource *conflict;
691
692 conflict = __insert_resource(root, new);
693 if (!conflict)
694 break;
695 if (conflict == root)
696 break;
697
698 /* Ok, expand resource to cover the conflict, then try again .. */
699 if (conflict->start < new->start)
700 new->start = conflict->start;
701 if (conflict->end > new->end)
702 new->end = conflict->end;
703
704 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
705 }
706 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Toshi Kaniae8e3a92013-04-29 15:08:17 -0700709static int __adjust_resource(struct resource *res, resource_size_t start,
710 resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct resource *tmp, *parent = res->parent;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700713 resource_size_t end = start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int result = -EBUSY;
715
Yinghai Lu82ec90e2012-05-17 18:51:11 -0700716 if (!parent)
717 goto skip;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if ((start < parent->start) || (end > parent->end))
720 goto out;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (res->sibling && (res->sibling->start <= end))
723 goto out;
724
725 tmp = parent->child;
726 if (tmp != res) {
727 while (tmp->sibling != res)
728 tmp = tmp->sibling;
729 if (start <= tmp->end)
730 goto out;
731 }
732
Yinghai Lu82ec90e2012-05-17 18:51:11 -0700733skip:
734 for (tmp = res->child; tmp; tmp = tmp->sibling)
735 if ((tmp->start < start) || (tmp->end > end))
736 goto out;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 res->start = start;
739 res->end = end;
740 result = 0;
741
742 out:
Toshi Kaniae8e3a92013-04-29 15:08:17 -0700743 return result;
744}
745
746/**
747 * adjust_resource - modify a resource's start and size
748 * @res: resource to modify
749 * @start: new start value
750 * @size: new size
751 *
752 * Given an existing resource, change its start and size to match the
753 * arguments. Returns 0 on success, -EBUSY if it can't fit.
754 * Existing children of the resource are assumed to be immutable.
755 */
756int adjust_resource(struct resource *res, resource_size_t start,
757 resource_size_t size)
758{
759 int result;
760
761 write_lock(&resource_lock);
762 result = __adjust_resource(res, start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 write_unlock(&resource_lock);
764 return result;
765}
Cong Wang24105742012-02-03 21:42:39 +0800766EXPORT_SYMBOL(adjust_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Yinghai Lu268364a2008-09-04 21:02:44 +0200768static void __init __reserve_region_with_split(struct resource *root,
769 resource_size_t start, resource_size_t end,
770 const char *name)
771{
772 struct resource *parent = root;
773 struct resource *conflict;
Linus Torvalds42c02022008-11-01 09:53:58 -0700774 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
T Makphaibulchoke4965f562012-10-04 17:16:55 -0700775 struct resource *next_res = NULL;
Yinghai Lu268364a2008-09-04 21:02:44 +0200776
777 if (!res)
778 return;
779
780 res->name = name;
781 res->start = start;
782 res->end = end;
783 res->flags = IORESOURCE_BUSY;
784
T Makphaibulchoke4965f562012-10-04 17:16:55 -0700785 while (1) {
Yinghai Lu268364a2008-09-04 21:02:44 +0200786
T Makphaibulchoke4965f562012-10-04 17:16:55 -0700787 conflict = __request_resource(parent, res);
788 if (!conflict) {
789 if (!next_res)
790 break;
791 res = next_res;
792 next_res = NULL;
793 continue;
794 }
Yinghai Lu268364a2008-09-04 21:02:44 +0200795
T Makphaibulchoke4965f562012-10-04 17:16:55 -0700796 /* conflict covered whole area */
797 if (conflict->start <= res->start &&
798 conflict->end >= res->end) {
799 kfree(res);
800 WARN_ON(next_res);
801 break;
802 }
Yinghai Lu268364a2008-09-04 21:02:44 +0200803
T Makphaibulchoke4965f562012-10-04 17:16:55 -0700804 /* failed, split and try again */
805 if (conflict->start > res->start) {
806 end = res->end;
807 res->end = conflict->start - 1;
808 if (conflict->end < end) {
809 next_res = kzalloc(sizeof(*next_res),
810 GFP_ATOMIC);
811 if (!next_res) {
812 kfree(res);
813 break;
814 }
815 next_res->name = name;
816 next_res->start = conflict->end + 1;
817 next_res->end = end;
818 next_res->flags = IORESOURCE_BUSY;
819 }
820 } else {
821 res->start = conflict->end + 1;
822 }
823 }
824
Yinghai Lu268364a2008-09-04 21:02:44 +0200825}
826
Paul Mundtbea92112008-10-22 19:31:11 +0900827void __init reserve_region_with_split(struct resource *root,
Yinghai Lu268364a2008-09-04 21:02:44 +0200828 resource_size_t start, resource_size_t end,
829 const char *name)
830{
Octavian Purdila65fed8f2012-07-30 14:42:58 -0700831 int abort = 0;
832
Yinghai Lu268364a2008-09-04 21:02:44 +0200833 write_lock(&resource_lock);
Octavian Purdila65fed8f2012-07-30 14:42:58 -0700834 if (root->start > start || root->end < end) {
835 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
836 (unsigned long long)start, (unsigned long long)end,
837 root);
838 if (start > root->end || end < root->start)
839 abort = 1;
840 else {
841 if (end > root->end)
842 end = root->end;
843 if (start < root->start)
844 start = root->start;
845 pr_err("fixing request to [0x%llx-0x%llx]\n",
846 (unsigned long long)start,
847 (unsigned long long)end);
848 }
849 dump_stack();
850 }
851 if (!abort)
852 __reserve_region_with_split(root, start, end, name);
Yinghai Lu268364a2008-09-04 21:02:44 +0200853 write_unlock(&resource_lock);
854}
855
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400856/**
857 * resource_alignment - calculate resource's alignment
858 * @res: resource pointer
859 *
860 * Returns alignment on success, 0 (invalid alignment) on failure.
861 */
862resource_size_t resource_alignment(struct resource *res)
863{
864 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
865 case IORESOURCE_SIZEALIGN:
Magnus Damm1a4e5642008-07-29 22:32:57 -0700866 return resource_size(res);
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400867 case IORESOURCE_STARTALIGN:
868 return res->start;
869 default:
870 return 0;
871 }
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * This is compatibility stuff for IO resources.
876 *
877 * Note how this, unlike the above, knows about
878 * the IO flag meanings (busy etc).
879 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700880 * request_region creates a new busy region.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700882 * check_region returns non-zero if the area is already busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700884 * release_region releases a matching busy region.
885 */
886
Alan Cox8b6d0432010-03-29 19:38:00 +0200887static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
888
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700889/**
890 * __request_region - create a new busy resource region
891 * @parent: parent resource descriptor
892 * @start: resource start address
893 * @n: resource region size
894 * @name: reserving caller's ID string
Randy Dunlap6ae301e2009-01-15 13:51:01 -0800895 * @flags: IO resource flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700897struct resource * __request_region(struct resource *parent,
898 resource_size_t start, resource_size_t n,
Arjan van de Vene8de1482008-10-22 19:55:31 -0700899 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Alan Cox8b6d0432010-03-29 19:38:00 +0200901 DECLARE_WAITQUEUE(wait, current);
Pekka J Enbergdd392712005-09-06 15:18:31 -0700902 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700904 if (!res)
905 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700907 res->name = name;
908 res->start = start;
909 res->end = start + n - 1;
910 res->flags = IORESOURCE_BUSY;
Arjan van de Vene8de1482008-10-22 19:55:31 -0700911 res->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700913 write_lock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700915 for (;;) {
916 struct resource *conflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700918 conflict = __request_resource(parent, res);
919 if (!conflict)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 break;
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700921 if (conflict != parent) {
922 parent = conflict;
923 if (!(conflict->flags & IORESOURCE_BUSY))
924 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Alan Cox8b6d0432010-03-29 19:38:00 +0200926 if (conflict->flags & flags & IORESOURCE_MUXED) {
927 add_wait_queue(&muxed_resource_wait, &wait);
928 write_unlock(&resource_lock);
929 set_current_state(TASK_UNINTERRUPTIBLE);
930 schedule();
931 remove_wait_queue(&muxed_resource_wait, &wait);
932 write_lock(&resource_lock);
933 continue;
934 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700935 /* Uhhuh, that didn't work out.. */
936 kfree(res);
937 res = NULL;
938 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700940 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return res;
942}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943EXPORT_SYMBOL(__request_region);
944
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700945/**
946 * __check_region - check if a resource region is busy or free
947 * @parent: parent resource descriptor
948 * @start: resource start address
949 * @n: resource region size
950 *
951 * Returns 0 if the region is free at the moment it is checked,
952 * returns %-EBUSY if the region is busy.
953 *
954 * NOTE:
955 * This function is deprecated because its use is racy.
956 * Even if it returns 0, a subsequent call to request_region()
957 * may fail because another driver etc. just allocated the region.
958 * Do NOT use it. It will be removed from the kernel.
959 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700960int __check_region(struct resource *parent, resource_size_t start,
961 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 struct resource * res;
964
Arjan van de Vene8de1482008-10-22 19:55:31 -0700965 res = __request_region(parent, start, n, "check-region", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!res)
967 return -EBUSY;
968
969 release_resource(res);
970 kfree(res);
971 return 0;
972}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973EXPORT_SYMBOL(__check_region);
974
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700975/**
976 * __release_region - release a previously reserved resource region
977 * @parent: parent resource descriptor
978 * @start: resource start address
979 * @n: resource region size
980 *
981 * The described resource region must match a currently busy region.
982 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700983void __release_region(struct resource *parent, resource_size_t start,
984 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct resource **p;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700987 resource_size_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 p = &parent->child;
990 end = start + n - 1;
991
992 write_lock(&resource_lock);
993
994 for (;;) {
995 struct resource *res = *p;
996
997 if (!res)
998 break;
999 if (res->start <= start && res->end >= end) {
1000 if (!(res->flags & IORESOURCE_BUSY)) {
1001 p = &res->child;
1002 continue;
1003 }
1004 if (res->start != start || res->end != end)
1005 break;
1006 *p = res->sibling;
1007 write_unlock(&resource_lock);
Alan Cox8b6d0432010-03-29 19:38:00 +02001008 if (res->flags & IORESOURCE_MUXED)
1009 wake_up(&muxed_resource_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 kfree(res);
1011 return;
1012 }
1013 p = &res->sibling;
1014 }
1015
1016 write_unlock(&resource_lock);
1017
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -07001018 printk(KERN_WARNING "Trying to free nonexistent resource "
1019 "<%016llx-%016llx>\n", (unsigned long long)start,
1020 (unsigned long long)end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022EXPORT_SYMBOL(__release_region);
1023
Toshi Kani825f7872013-04-29 15:08:19 -07001024#ifdef CONFIG_MEMORY_HOTREMOVE
1025/**
1026 * release_mem_region_adjustable - release a previously reserved memory region
1027 * @parent: parent resource descriptor
1028 * @start: resource start address
1029 * @size: resource region size
1030 *
1031 * This interface is intended for memory hot-delete. The requested region
1032 * is released from a currently busy memory resource. The requested region
1033 * must either match exactly or fit into a single busy resource entry. In
1034 * the latter case, the remaining resource is adjusted accordingly.
1035 * Existing children of the busy memory resource must be immutable in the
1036 * request.
1037 *
1038 * Note:
1039 * - Additional release conditions, such as overlapping region, can be
1040 * supported after they are confirmed as valid cases.
1041 * - When a busy memory resource gets split into two entries, the code
1042 * assumes that all children remain in the lower address entry for
1043 * simplicity. Enhance this logic when necessary.
1044 */
1045int release_mem_region_adjustable(struct resource *parent,
1046 resource_size_t start, resource_size_t size)
1047{
1048 struct resource **p;
1049 struct resource *res;
1050 struct resource *new_res;
1051 resource_size_t end;
1052 int ret = -EINVAL;
1053
1054 end = start + size - 1;
1055 if ((start < parent->start) || (end > parent->end))
1056 return ret;
1057
1058 /* The kzalloc() result gets checked later */
1059 new_res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1060
1061 p = &parent->child;
1062 write_lock(&resource_lock);
1063
1064 while ((res = *p)) {
1065 if (res->start >= end)
1066 break;
1067
1068 /* look for the next resource if it does not fit into */
1069 if (res->start > start || res->end < end) {
1070 p = &res->sibling;
1071 continue;
1072 }
1073
1074 if (!(res->flags & IORESOURCE_MEM))
1075 break;
1076
1077 if (!(res->flags & IORESOURCE_BUSY)) {
1078 p = &res->child;
1079 continue;
1080 }
1081
1082 /* found the target resource; let's adjust accordingly */
1083 if (res->start == start && res->end == end) {
1084 /* free the whole entry */
1085 *p = res->sibling;
1086 kfree(res);
1087 ret = 0;
1088 } else if (res->start == start && res->end != end) {
1089 /* adjust the start */
1090 ret = __adjust_resource(res, end + 1,
1091 res->end - end);
1092 } else if (res->start != start && res->end == end) {
1093 /* adjust the end */
1094 ret = __adjust_resource(res, res->start,
1095 start - res->start);
1096 } else {
1097 /* split into two entries */
1098 if (!new_res) {
1099 ret = -ENOMEM;
1100 break;
1101 }
1102 new_res->name = res->name;
1103 new_res->start = end + 1;
1104 new_res->end = res->end;
1105 new_res->flags = res->flags;
1106 new_res->parent = res->parent;
1107 new_res->sibling = res->sibling;
1108 new_res->child = NULL;
1109
1110 ret = __adjust_resource(res, res->start,
1111 start - res->start);
1112 if (ret)
1113 break;
1114 res->sibling = new_res;
1115 new_res = NULL;
1116 }
1117
1118 break;
1119 }
1120
1121 write_unlock(&resource_lock);
1122 kfree(new_res);
1123 return ret;
1124}
1125#endif /* CONFIG_MEMORY_HOTREMOVE */
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/*
Tejun Heo9ac78492007-01-20 16:00:26 +09001128 * Managed region resource
1129 */
1130struct region_devres {
1131 struct resource *parent;
1132 resource_size_t start;
1133 resource_size_t n;
1134};
1135
1136static void devm_region_release(struct device *dev, void *res)
1137{
1138 struct region_devres *this = res;
1139
1140 __release_region(this->parent, this->start, this->n);
1141}
1142
1143static int devm_region_match(struct device *dev, void *res, void *match_data)
1144{
1145 struct region_devres *this = res, *match = match_data;
1146
1147 return this->parent == match->parent &&
1148 this->start == match->start && this->n == match->n;
1149}
1150
1151struct resource * __devm_request_region(struct device *dev,
1152 struct resource *parent, resource_size_t start,
1153 resource_size_t n, const char *name)
1154{
1155 struct region_devres *dr = NULL;
1156 struct resource *res;
1157
1158 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1159 GFP_KERNEL);
1160 if (!dr)
1161 return NULL;
1162
1163 dr->parent = parent;
1164 dr->start = start;
1165 dr->n = n;
1166
Arjan van de Vene8de1482008-10-22 19:55:31 -07001167 res = __request_region(parent, start, n, name, 0);
Tejun Heo9ac78492007-01-20 16:00:26 +09001168 if (res)
1169 devres_add(dev, dr);
1170 else
1171 devres_free(dr);
1172
1173 return res;
1174}
1175EXPORT_SYMBOL(__devm_request_region);
1176
1177void __devm_release_region(struct device *dev, struct resource *parent,
1178 resource_size_t start, resource_size_t n)
1179{
1180 struct region_devres match_data = { parent, start, n };
1181
1182 __release_region(parent, start, n);
1183 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1184 &match_data));
1185}
1186EXPORT_SYMBOL(__devm_release_region);
1187
1188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * Called from init/main.c to reserve IO ports.
1190 */
1191#define MAXRESERVE 4
1192static int __init reserve_setup(char *str)
1193{
1194 static int reserved;
1195 static struct resource reserve[MAXRESERVE];
1196
1197 for (;;) {
Zhang Rui8bc1ad72009-06-30 11:41:31 -07001198 unsigned int io_start, io_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int x = reserved;
1200
1201 if (get_option (&str, &io_start) != 2)
1202 break;
1203 if (get_option (&str, &io_num) == 0)
1204 break;
1205 if (x < MAXRESERVE) {
1206 struct resource *res = reserve + x;
1207 res->name = "reserved";
1208 res->start = io_start;
1209 res->end = io_start + io_num - 1;
1210 res->flags = IORESOURCE_BUSY;
1211 res->child = NULL;
1212 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
1213 reserved = x+1;
1214 }
1215 }
1216 return 1;
1217}
1218
1219__setup("reserve=", reserve_setup);
Suresh Siddha379daf62008-09-25 18:43:34 -07001220
1221/*
1222 * Check if the requested addr and size spans more than any slot in the
1223 * iomem resource tree.
1224 */
1225int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1226{
1227 struct resource *p = &iomem_resource;
1228 int err = 0;
1229 loff_t l;
1230
1231 read_lock(&resource_lock);
1232 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1233 /*
1234 * We can probably skip the resources without
1235 * IORESOURCE_IO attribute?
1236 */
1237 if (p->start >= addr + size)
1238 continue;
1239 if (p->end < addr)
1240 continue;
Suresh Siddhad68612b2008-10-28 11:45:42 -07001241 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1242 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
Suresh Siddha379daf62008-09-25 18:43:34 -07001243 continue;
Arjan van de Ven3ac52662008-12-13 09:15:27 -08001244 /*
1245 * if a resource is "BUSY", it's not a hardware resource
1246 * but a driver mapping of such a resource; we don't want
1247 * to warn for those; some drivers legitimately map only
1248 * partial hardware resources. (example: vesafb)
1249 */
1250 if (p->flags & IORESOURCE_BUSY)
1251 continue;
1252
Suresh Siddha379daf62008-09-25 18:43:34 -07001253 printk(KERN_WARNING "resource map sanity check conflict: "
1254 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
Ingo Molnar13eb8372008-09-26 10:10:12 +02001255 (unsigned long long)addr,
1256 (unsigned long long)(addr + size - 1),
1257 (unsigned long long)p->start,
1258 (unsigned long long)p->end,
1259 p->name);
Suresh Siddha379daf62008-09-25 18:43:34 -07001260 err = -1;
1261 break;
1262 }
1263 read_unlock(&resource_lock);
1264
1265 return err;
1266}
Arjan van de Vene8de1482008-10-22 19:55:31 -07001267
1268#ifdef CONFIG_STRICT_DEVMEM
1269static int strict_iomem_checks = 1;
1270#else
1271static int strict_iomem_checks;
1272#endif
1273
1274/*
1275 * check if an address is reserved in the iomem resource tree
1276 * returns 1 if reserved, 0 if not reserved.
1277 */
1278int iomem_is_exclusive(u64 addr)
1279{
1280 struct resource *p = &iomem_resource;
1281 int err = 0;
1282 loff_t l;
1283 int size = PAGE_SIZE;
1284
1285 if (!strict_iomem_checks)
1286 return 0;
1287
1288 addr = addr & PAGE_MASK;
1289
1290 read_lock(&resource_lock);
1291 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1292 /*
1293 * We can probably skip the resources without
1294 * IORESOURCE_IO attribute?
1295 */
1296 if (p->start >= addr + size)
1297 break;
1298 if (p->end < addr)
1299 continue;
1300 if (p->flags & IORESOURCE_BUSY &&
1301 p->flags & IORESOURCE_EXCLUSIVE) {
1302 err = 1;
1303 break;
1304 }
1305 }
1306 read_unlock(&resource_lock);
1307
1308 return err;
1309}
1310
1311static int __init strict_iomem(char *str)
1312{
1313 if (strstr(str, "relaxed"))
1314 strict_iomem_checks = 0;
1315 if (strstr(str, "strict"))
1316 strict_iomem_checks = 1;
1317 return 1;
1318}
1319
1320__setup("iomem=", strict_iomem);