blob: 4e9d87fd7bc5609f86d2cd7b96f8fb3889dc63b7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
12#include <linux/ioport.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/fs.h>
17#include <linux/proc_fs.h>
18#include <linux/seq_file.h>
Tejun Heo9ac78492007-01-20 16:00:26 +090019#include <linux/device.h>
Suresh Siddhad68612b2008-10-28 11:45:42 -070020#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/io.h>
22
23
24struct resource ioport_resource = {
25 .name = "PCI IO",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070026 .start = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 .end = IO_SPACE_LIMIT,
28 .flags = IORESOURCE_IO,
29};
Linus Torvalds1da177e2005-04-16 15:20:36 -070030EXPORT_SYMBOL(ioport_resource);
31
32struct resource iomem_resource = {
33 .name = "PCI mem",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070034 .start = 0,
35 .end = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .flags = IORESOURCE_MEM,
37};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038EXPORT_SYMBOL(iomem_resource);
39
40static DEFINE_RWLOCK(resource_lock);
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static void *r_next(struct seq_file *m, void *v, loff_t *pos)
43{
44 struct resource *p = v;
45 (*pos)++;
46 if (p->child)
47 return p->child;
48 while (!p->sibling && p->parent)
49 p = p->parent;
50 return p->sibling;
51}
52
Ingo Molnar13eb8372008-09-26 10:10:12 +020053#ifdef CONFIG_PROC_FS
54
55enum { MAX_IORES_LEVEL = 5 };
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void *r_start(struct seq_file *m, loff_t *pos)
58 __acquires(resource_lock)
59{
60 struct resource *p = m->private;
61 loff_t l = 0;
62 read_lock(&resource_lock);
63 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
64 ;
65 return p;
66}
67
68static void r_stop(struct seq_file *m, void *v)
69 __releases(resource_lock)
70{
71 read_unlock(&resource_lock);
72}
73
74static int r_show(struct seq_file *m, void *v)
75{
76 struct resource *root = m->private;
77 struct resource *r = v, *p;
78 int width = root->end < 0x10000 ? 4 : 8;
79 int depth;
80
81 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
82 if (p->parent == root)
83 break;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -070084 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 depth * 2, "",
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -070086 width, (unsigned long long) r->start,
87 width, (unsigned long long) r->end,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 r->name ? r->name : "<BAD>");
89 return 0;
90}
91
Helge Deller15ad7cd2006-12-06 20:40:36 -080092static const struct seq_operations resource_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .start = r_start,
94 .next = r_next,
95 .stop = r_stop,
96 .show = r_show,
97};
98
99static int ioports_open(struct inode *inode, struct file *file)
100{
101 int res = seq_open(file, &resource_op);
102 if (!res) {
103 struct seq_file *m = file->private_data;
104 m->private = &ioport_resource;
105 }
106 return res;
107}
108
109static int iomem_open(struct inode *inode, struct file *file)
110{
111 int res = seq_open(file, &resource_op);
112 if (!res) {
113 struct seq_file *m = file->private_data;
114 m->private = &iomem_resource;
115 }
116 return res;
117}
118
Helge Deller15ad7cd2006-12-06 20:40:36 -0800119static const struct file_operations proc_ioports_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .open = ioports_open,
121 .read = seq_read,
122 .llseek = seq_lseek,
123 .release = seq_release,
124};
125
Helge Deller15ad7cd2006-12-06 20:40:36 -0800126static const struct file_operations proc_iomem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .open = iomem_open,
128 .read = seq_read,
129 .llseek = seq_lseek,
130 .release = seq_release,
131};
132
133static int __init ioresources_init(void)
134{
Denis V. Lunevc33fff02008-04-29 01:02:31 -0700135 proc_create("ioports", 0, NULL, &proc_ioports_operations);
136 proc_create("iomem", 0, NULL, &proc_iomem_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138}
139__initcall(ioresources_init);
140
141#endif /* CONFIG_PROC_FS */
142
143/* Return the conflict entry if you can't request it */
144static struct resource * __request_resource(struct resource *root, struct resource *new)
145{
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700146 resource_size_t start = new->start;
147 resource_size_t end = new->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct resource *tmp, **p;
149
150 if (end < start)
151 return root;
152 if (start < root->start)
153 return root;
154 if (end > root->end)
155 return root;
156 p = &root->child;
157 for (;;) {
158 tmp = *p;
159 if (!tmp || tmp->start > end) {
160 new->sibling = tmp;
161 *p = new;
162 new->parent = root;
163 return NULL;
164 }
165 p = &tmp->sibling;
166 if (tmp->end < start)
167 continue;
168 return tmp;
169 }
170}
171
172static int __release_resource(struct resource *old)
173{
174 struct resource *tmp, **p;
175
176 p = &old->parent->child;
177 for (;;) {
178 tmp = *p;
179 if (!tmp)
180 break;
181 if (tmp == old) {
182 *p = tmp->sibling;
183 old->parent = NULL;
184 return 0;
185 }
186 p = &tmp->sibling;
187 }
188 return -EINVAL;
189}
190
Yinghai Lu5eeec0e2009-12-22 15:02:22 -0800191static void __release_child_resources(struct resource *r)
192{
193 struct resource *tmp, *p;
194 resource_size_t size;
195
196 p = r->child;
197 r->child = NULL;
198 while (p) {
199 tmp = p;
200 p = p->sibling;
201
202 tmp->parent = NULL;
203 tmp->sibling = NULL;
204 __release_child_resources(tmp);
205
206 printk(KERN_DEBUG "release child resource %pR\n", tmp);
207 /* need to restore size, and keep flags */
208 size = resource_size(tmp);
209 tmp->start = 0;
210 tmp->end = size - 1;
211 }
212}
213
214void release_child_resources(struct resource *r)
215{
216 write_lock(&resource_lock);
217 __release_child_resources(r);
218 write_unlock(&resource_lock);
219}
220
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700221/**
222 * request_resource - request and reserve an I/O or memory resource
223 * @root: root resource descriptor
224 * @new: resource descriptor desired by caller
225 *
226 * Returns 0 for success, negative error code on error.
227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228int request_resource(struct resource *root, struct resource *new)
229{
230 struct resource *conflict;
231
232 write_lock(&resource_lock);
233 conflict = __request_resource(root, new);
234 write_unlock(&resource_lock);
235 return conflict ? -EBUSY : 0;
236}
237
238EXPORT_SYMBOL(request_resource);
239
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700240/**
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700241 * release_resource - release a previously reserved resource
242 * @old: resource pointer
243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244int release_resource(struct resource *old)
245{
246 int retval;
247
248 write_lock(&resource_lock);
249 retval = __release_resource(old);
250 write_unlock(&resource_lock);
251 return retval;
252}
253
254EXPORT_SYMBOL(release_resource);
255
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700256#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700257/*
258 * Finds the lowest memory reosurce exists within [res->start.res->end)
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700259 * the caller must specify res->start, res->end, res->flags and "name".
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700260 * If found, returns 0, res is overwritten, if not found, returns -1.
261 */
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700262static int find_next_system_ram(struct resource *res, char *name)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700263{
264 resource_size_t start, end;
265 struct resource *p;
266
267 BUG_ON(!res);
268
269 start = res->start;
270 end = res->end;
KAMEZAWA Hiroyuki58c1b5b2006-08-05 12:15:01 -0700271 BUG_ON(start >= end);
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700272
273 read_lock(&resource_lock);
274 for (p = iomem_resource.child; p ; p = p->sibling) {
275 /* system ram is just marked as IORESOURCE_MEM */
276 if (p->flags != res->flags)
277 continue;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700278 if (name && strcmp(p->name, name))
279 continue;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700280 if (p->start > end) {
281 p = NULL;
282 break;
283 }
KAMEZAWA Hiroyuki58c1b5b2006-08-05 12:15:01 -0700284 if ((p->end >= start) && (p->start < end))
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700285 break;
286 }
287 read_unlock(&resource_lock);
288 if (!p)
289 return -1;
290 /* copy data */
KAMEZAWA Hiroyuki0f04ab52006-08-05 12:14:59 -0700291 if (res->start < p->start)
292 res->start = p->start;
293 if (res->end > p->end)
294 res->end = p->end;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700295 return 0;
296}
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700297
298/*
299 * This function calls callback against all memory range of "System RAM"
300 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
301 * Now, this function is only for "System RAM".
302 */
303int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
304 void *arg, int (*func)(unsigned long, unsigned long, void *))
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700305{
306 struct resource res;
307 unsigned long pfn, len;
308 u64 orig_end;
309 int ret = -1;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700310
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700311 res.start = (u64) start_pfn << PAGE_SHIFT;
312 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -0800313 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700314 orig_end = res.end;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700315 while ((res.start < res.end) &&
316 (find_next_system_ram(&res, "System RAM") >= 0)) {
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700317 pfn = (unsigned long)(res.start >> PAGE_SHIFT);
318 len = (unsigned long)((res.end + 1 - res.start) >> PAGE_SHIFT);
319 ret = (*func)(pfn, len, arg);
320 if (ret)
321 break;
322 res.start = res.end + 1;
323 res.end = orig_end;
324 }
325 return ret;
326}
327
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700328#endif
329
Wu Fengguang61ef2482010-01-22 16:16:19 +0800330static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
331{
332 return 1;
333}
334/*
335 * This generic page_is_ram() returns true if specified address is
336 * registered as "System RAM" in iomem_resource list.
337 */
Andrew Mortone5273002010-01-26 16:31:19 -0800338int __weak page_is_ram(unsigned long pfn)
Wu Fengguang61ef2482010-01-22 16:16:19 +0800339{
340 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/*
344 * Find empty slot in the resource tree given range and alignment.
345 */
346static int find_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700347 resource_size_t size, resource_size_t min,
348 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100349 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100350 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100351 resource_size_t,
352 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 void *alignf_data)
354{
355 struct resource *this = root->child;
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100356 struct resource tmp = *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100358 tmp.start = root->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
360 * Skip past an allocated resource that starts at 0, since the assignment
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100361 * of this->start - 1 to tmp->end below would cause an underflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
363 if (this && this->start == 0) {
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100364 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 this = this->sibling;
366 }
367 for(;;) {
368 if (this)
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100369 tmp.end = this->start - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 else
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100371 tmp.end = root->end;
372 if (tmp.start < min)
373 tmp.start = min;
374 if (tmp.end > max)
375 tmp.end = max;
376 tmp.start = ALIGN(tmp.start, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (alignf)
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100378 tmp.start = alignf(alignf_data, &tmp, size, align);
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100379 if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
380 new->start = tmp.start;
381 new->end = tmp.start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383 }
384 if (!this)
385 break;
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100386 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 this = this->sibling;
388 }
389 return -EBUSY;
390}
391
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700392/**
393 * allocate_resource - allocate empty slot in the resource tree given range & alignment
394 * @root: root resource descriptor
395 * @new: resource descriptor desired by caller
396 * @size: requested resource region size
397 * @min: minimum size to allocate
398 * @max: maximum size to allocate
399 * @align: alignment requested, in bytes
400 * @alignf: alignment function, optional, called if not NULL
401 * @alignf_data: arbitrary data to pass to the @alignf function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
403int allocate_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700404 resource_size_t size, resource_size_t min,
405 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100406 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100407 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100408 resource_size_t,
409 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 void *alignf_data)
411{
412 int err;
413
414 write_lock(&resource_lock);
415 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
416 if (err >= 0 && __request_resource(root, new))
417 err = -EBUSY;
418 write_unlock(&resource_lock);
419 return err;
420}
421
422EXPORT_SYMBOL(allocate_resource);
423
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700424/*
425 * Insert a resource into the resource tree. If successful, return NULL,
426 * otherwise return the conflicting resource (compare to __request_resource())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700428static struct resource * __insert_resource(struct resource *parent, struct resource *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct resource *first, *next;
431
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700432 for (;; parent = first) {
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700433 first = __request_resource(parent, new);
434 if (!first)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700435 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700437 if (first == parent)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700438 return first;
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700439
440 if ((first->start > new->start) || (first->end < new->end))
441 break;
442 if ((first->start == new->start) && (first->end == new->end))
443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 for (next = first; ; next = next->sibling) {
447 /* Partial overlap? Bad, and unfixable */
448 if (next->start < new->start || next->end > new->end)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700449 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (!next->sibling)
451 break;
452 if (next->sibling->start > new->end)
453 break;
454 }
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 new->parent = parent;
457 new->sibling = next->sibling;
458 new->child = first;
459
460 next->sibling = NULL;
461 for (next = first; next; next = next->sibling)
462 next->parent = new;
463
464 if (parent->child == first) {
465 parent->child = new;
466 } else {
467 next = parent->child;
468 while (next->sibling != first)
469 next = next->sibling;
470 next->sibling = new;
471 }
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700472 return NULL;
473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700475/**
476 * insert_resource - Inserts a resource in the resource tree
477 * @parent: parent of the new resource
478 * @new: new resource to insert
479 *
480 * Returns 0 on success, -EBUSY if the resource can't be inserted.
481 *
482 * This function is equivalent to request_resource when no conflict
483 * happens. If a conflict happens, and the conflicting resources
484 * entirely fit within the range of the new resource, then the new
485 * resource is inserted and the conflicting resources become children of
486 * the new resource.
487 */
488int insert_resource(struct resource *parent, struct resource *new)
489{
490 struct resource *conflict;
491
492 write_lock(&resource_lock);
493 conflict = __insert_resource(parent, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 write_unlock(&resource_lock);
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700495 return conflict ? -EBUSY : 0;
496}
497
498/**
499 * insert_resource_expand_to_fit - Insert a resource into the resource tree
Randy Dunlap6781f4a2008-08-31 20:31:55 -0700500 * @root: root resource descriptor
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700501 * @new: new resource to insert
502 *
503 * Insert a resource into the resource tree, possibly expanding it in order
504 * to make it encompass any conflicting resources.
505 */
506void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
507{
508 if (new->parent)
509 return;
510
511 write_lock(&resource_lock);
512 for (;;) {
513 struct resource *conflict;
514
515 conflict = __insert_resource(root, new);
516 if (!conflict)
517 break;
518 if (conflict == root)
519 break;
520
521 /* Ok, expand resource to cover the conflict, then try again .. */
522 if (conflict->start < new->start)
523 new->start = conflict->start;
524 if (conflict->end > new->end)
525 new->end = conflict->end;
526
527 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
528 }
529 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700532/**
533 * adjust_resource - modify a resource's start and size
534 * @res: resource to modify
535 * @start: new start value
536 * @size: new size
537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Given an existing resource, change its start and size to match the
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700539 * arguments. Returns 0 on success, -EBUSY if it can't fit.
540 * Existing children of the resource are assumed to be immutable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700542int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct resource *tmp, *parent = res->parent;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700545 resource_size_t end = start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int result = -EBUSY;
547
548 write_lock(&resource_lock);
549
550 if ((start < parent->start) || (end > parent->end))
551 goto out;
552
553 for (tmp = res->child; tmp; tmp = tmp->sibling) {
554 if ((tmp->start < start) || (tmp->end > end))
555 goto out;
556 }
557
558 if (res->sibling && (res->sibling->start <= end))
559 goto out;
560
561 tmp = parent->child;
562 if (tmp != res) {
563 while (tmp->sibling != res)
564 tmp = tmp->sibling;
565 if (start <= tmp->end)
566 goto out;
567 }
568
569 res->start = start;
570 res->end = end;
571 result = 0;
572
573 out:
574 write_unlock(&resource_lock);
575 return result;
576}
577
Yinghai Lu268364a2008-09-04 21:02:44 +0200578static void __init __reserve_region_with_split(struct resource *root,
579 resource_size_t start, resource_size_t end,
580 const char *name)
581{
582 struct resource *parent = root;
583 struct resource *conflict;
Linus Torvalds42c02022008-11-01 09:53:58 -0700584 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
Yinghai Lu268364a2008-09-04 21:02:44 +0200585
586 if (!res)
587 return;
588
589 res->name = name;
590 res->start = start;
591 res->end = end;
592 res->flags = IORESOURCE_BUSY;
593
Linus Torvaldsff542502009-04-18 21:44:24 -0700594 conflict = __request_resource(parent, res);
595 if (!conflict)
596 return;
Yinghai Lu268364a2008-09-04 21:02:44 +0200597
Linus Torvaldsff542502009-04-18 21:44:24 -0700598 /* failed, split and try again */
599 kfree(res);
Yinghai Lu268364a2008-09-04 21:02:44 +0200600
Linus Torvaldsff542502009-04-18 21:44:24 -0700601 /* conflict covered whole area */
602 if (conflict->start <= start && conflict->end >= end)
603 return;
Yinghai Lu268364a2008-09-04 21:02:44 +0200604
Linus Torvaldsff542502009-04-18 21:44:24 -0700605 if (conflict->start > start)
606 __reserve_region_with_split(root, start, conflict->start-1, name);
607 if (conflict->end < end)
608 __reserve_region_with_split(root, conflict->end+1, end, name);
Yinghai Lu268364a2008-09-04 21:02:44 +0200609}
610
Paul Mundtbea92112008-10-22 19:31:11 +0900611void __init reserve_region_with_split(struct resource *root,
Yinghai Lu268364a2008-09-04 21:02:44 +0200612 resource_size_t start, resource_size_t end,
613 const char *name)
614{
615 write_lock(&resource_lock);
616 __reserve_region_with_split(root, start, end, name);
617 write_unlock(&resource_lock);
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620EXPORT_SYMBOL(adjust_resource);
621
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400622/**
623 * resource_alignment - calculate resource's alignment
624 * @res: resource pointer
625 *
626 * Returns alignment on success, 0 (invalid alignment) on failure.
627 */
628resource_size_t resource_alignment(struct resource *res)
629{
630 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
631 case IORESOURCE_SIZEALIGN:
Magnus Damm1a4e5642008-07-29 22:32:57 -0700632 return resource_size(res);
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400633 case IORESOURCE_STARTALIGN:
634 return res->start;
635 default:
636 return 0;
637 }
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/*
641 * This is compatibility stuff for IO resources.
642 *
643 * Note how this, unlike the above, knows about
644 * the IO flag meanings (busy etc).
645 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700646 * request_region creates a new busy region.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700648 * check_region returns non-zero if the area is already busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700650 * release_region releases a matching busy region.
651 */
652
653/**
654 * __request_region - create a new busy resource region
655 * @parent: parent resource descriptor
656 * @start: resource start address
657 * @n: resource region size
658 * @name: reserving caller's ID string
Randy Dunlap6ae301e2009-01-15 13:51:01 -0800659 * @flags: IO resource flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700661struct resource * __request_region(struct resource *parent,
662 resource_size_t start, resource_size_t n,
Arjan van de Vene8de1482008-10-22 19:55:31 -0700663 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Pekka J Enbergdd392712005-09-06 15:18:31 -0700665 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700667 if (!res)
668 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700670 res->name = name;
671 res->start = start;
672 res->end = start + n - 1;
673 res->flags = IORESOURCE_BUSY;
Arjan van de Vene8de1482008-10-22 19:55:31 -0700674 res->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700676 write_lock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700678 for (;;) {
679 struct resource *conflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700681 conflict = __request_resource(parent, res);
682 if (!conflict)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700684 if (conflict != parent) {
685 parent = conflict;
686 if (!(conflict->flags & IORESOURCE_BUSY))
687 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700689
690 /* Uhhuh, that didn't work out.. */
691 kfree(res);
692 res = NULL;
693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700695 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return res;
697}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698EXPORT_SYMBOL(__request_region);
699
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700700/**
701 * __check_region - check if a resource region is busy or free
702 * @parent: parent resource descriptor
703 * @start: resource start address
704 * @n: resource region size
705 *
706 * Returns 0 if the region is free at the moment it is checked,
707 * returns %-EBUSY if the region is busy.
708 *
709 * NOTE:
710 * This function is deprecated because its use is racy.
711 * Even if it returns 0, a subsequent call to request_region()
712 * may fail because another driver etc. just allocated the region.
713 * Do NOT use it. It will be removed from the kernel.
714 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700715int __check_region(struct resource *parent, resource_size_t start,
716 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct resource * res;
719
Arjan van de Vene8de1482008-10-22 19:55:31 -0700720 res = __request_region(parent, start, n, "check-region", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!res)
722 return -EBUSY;
723
724 release_resource(res);
725 kfree(res);
726 return 0;
727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728EXPORT_SYMBOL(__check_region);
729
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700730/**
731 * __release_region - release a previously reserved resource region
732 * @parent: parent resource descriptor
733 * @start: resource start address
734 * @n: resource region size
735 *
736 * The described resource region must match a currently busy region.
737 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700738void __release_region(struct resource *parent, resource_size_t start,
739 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 struct resource **p;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700742 resource_size_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 p = &parent->child;
745 end = start + n - 1;
746
747 write_lock(&resource_lock);
748
749 for (;;) {
750 struct resource *res = *p;
751
752 if (!res)
753 break;
754 if (res->start <= start && res->end >= end) {
755 if (!(res->flags & IORESOURCE_BUSY)) {
756 p = &res->child;
757 continue;
758 }
759 if (res->start != start || res->end != end)
760 break;
761 *p = res->sibling;
762 write_unlock(&resource_lock);
763 kfree(res);
764 return;
765 }
766 p = &res->sibling;
767 }
768
769 write_unlock(&resource_lock);
770
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700771 printk(KERN_WARNING "Trying to free nonexistent resource "
772 "<%016llx-%016llx>\n", (unsigned long long)start,
773 (unsigned long long)end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775EXPORT_SYMBOL(__release_region);
776
777/*
Tejun Heo9ac78492007-01-20 16:00:26 +0900778 * Managed region resource
779 */
780struct region_devres {
781 struct resource *parent;
782 resource_size_t start;
783 resource_size_t n;
784};
785
786static void devm_region_release(struct device *dev, void *res)
787{
788 struct region_devres *this = res;
789
790 __release_region(this->parent, this->start, this->n);
791}
792
793static int devm_region_match(struct device *dev, void *res, void *match_data)
794{
795 struct region_devres *this = res, *match = match_data;
796
797 return this->parent == match->parent &&
798 this->start == match->start && this->n == match->n;
799}
800
801struct resource * __devm_request_region(struct device *dev,
802 struct resource *parent, resource_size_t start,
803 resource_size_t n, const char *name)
804{
805 struct region_devres *dr = NULL;
806 struct resource *res;
807
808 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
809 GFP_KERNEL);
810 if (!dr)
811 return NULL;
812
813 dr->parent = parent;
814 dr->start = start;
815 dr->n = n;
816
Arjan van de Vene8de1482008-10-22 19:55:31 -0700817 res = __request_region(parent, start, n, name, 0);
Tejun Heo9ac78492007-01-20 16:00:26 +0900818 if (res)
819 devres_add(dev, dr);
820 else
821 devres_free(dr);
822
823 return res;
824}
825EXPORT_SYMBOL(__devm_request_region);
826
827void __devm_release_region(struct device *dev, struct resource *parent,
828 resource_size_t start, resource_size_t n)
829{
830 struct region_devres match_data = { parent, start, n };
831
832 __release_region(parent, start, n);
833 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
834 &match_data));
835}
836EXPORT_SYMBOL(__devm_release_region);
837
838/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * Called from init/main.c to reserve IO ports.
840 */
841#define MAXRESERVE 4
842static int __init reserve_setup(char *str)
843{
844 static int reserved;
845 static struct resource reserve[MAXRESERVE];
846
847 for (;;) {
Zhang Rui8bc1ad72009-06-30 11:41:31 -0700848 unsigned int io_start, io_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 int x = reserved;
850
851 if (get_option (&str, &io_start) != 2)
852 break;
853 if (get_option (&str, &io_num) == 0)
854 break;
855 if (x < MAXRESERVE) {
856 struct resource *res = reserve + x;
857 res->name = "reserved";
858 res->start = io_start;
859 res->end = io_start + io_num - 1;
860 res->flags = IORESOURCE_BUSY;
861 res->child = NULL;
862 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
863 reserved = x+1;
864 }
865 }
866 return 1;
867}
868
869__setup("reserve=", reserve_setup);
Suresh Siddha379daf62008-09-25 18:43:34 -0700870
871/*
872 * Check if the requested addr and size spans more than any slot in the
873 * iomem resource tree.
874 */
875int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
876{
877 struct resource *p = &iomem_resource;
878 int err = 0;
879 loff_t l;
880
881 read_lock(&resource_lock);
882 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
883 /*
884 * We can probably skip the resources without
885 * IORESOURCE_IO attribute?
886 */
887 if (p->start >= addr + size)
888 continue;
889 if (p->end < addr)
890 continue;
Suresh Siddhad68612b2008-10-28 11:45:42 -0700891 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
892 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
Suresh Siddha379daf62008-09-25 18:43:34 -0700893 continue;
Arjan van de Ven3ac52662008-12-13 09:15:27 -0800894 /*
895 * if a resource is "BUSY", it's not a hardware resource
896 * but a driver mapping of such a resource; we don't want
897 * to warn for those; some drivers legitimately map only
898 * partial hardware resources. (example: vesafb)
899 */
900 if (p->flags & IORESOURCE_BUSY)
901 continue;
902
Suresh Siddha379daf62008-09-25 18:43:34 -0700903 printk(KERN_WARNING "resource map sanity check conflict: "
904 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
Ingo Molnar13eb8372008-09-26 10:10:12 +0200905 (unsigned long long)addr,
906 (unsigned long long)(addr + size - 1),
907 (unsigned long long)p->start,
908 (unsigned long long)p->end,
909 p->name);
Suresh Siddha379daf62008-09-25 18:43:34 -0700910 err = -1;
911 break;
912 }
913 read_unlock(&resource_lock);
914
915 return err;
916}
Arjan van de Vene8de1482008-10-22 19:55:31 -0700917
918#ifdef CONFIG_STRICT_DEVMEM
919static int strict_iomem_checks = 1;
920#else
921static int strict_iomem_checks;
922#endif
923
924/*
925 * check if an address is reserved in the iomem resource tree
926 * returns 1 if reserved, 0 if not reserved.
927 */
928int iomem_is_exclusive(u64 addr)
929{
930 struct resource *p = &iomem_resource;
931 int err = 0;
932 loff_t l;
933 int size = PAGE_SIZE;
934
935 if (!strict_iomem_checks)
936 return 0;
937
938 addr = addr & PAGE_MASK;
939
940 read_lock(&resource_lock);
941 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
942 /*
943 * We can probably skip the resources without
944 * IORESOURCE_IO attribute?
945 */
946 if (p->start >= addr + size)
947 break;
948 if (p->end < addr)
949 continue;
950 if (p->flags & IORESOURCE_BUSY &&
951 p->flags & IORESOURCE_EXCLUSIVE) {
952 err = 1;
953 break;
954 }
955 }
956 read_unlock(&resource_lock);
957
958 return err;
959}
960
961static int __init strict_iomem(char *str)
962{
963 if (strstr(str, "relaxed"))
964 strict_iomem_checks = 0;
965 if (strstr(str, "strict"))
966 strict_iomem_checks = 1;
967 return 1;
968}
969
970__setup("iomem=", strict_iomem);