blob: 716b6804077ef0988da621899dda9cb70a1ec76b [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>
Alan Cox8b6d0432010-03-29 19:38:00 +020018#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/seq_file.h>
Tejun Heo9ac78492007-01-20 16:00:26 +090020#include <linux/device.h>
Suresh Siddhad68612b2008-10-28 11:45:42 -070021#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/io.h>
23
24
25struct resource ioport_resource = {
26 .name = "PCI IO",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070027 .start = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 .end = IO_SPACE_LIMIT,
29 .flags = IORESOURCE_IO,
30};
Linus Torvalds1da177e2005-04-16 15:20:36 -070031EXPORT_SYMBOL(ioport_resource);
32
33struct resource iomem_resource = {
34 .name = "PCI mem",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070035 .start = 0,
36 .end = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .flags = IORESOURCE_MEM,
38};
Linus Torvalds1da177e2005-04-16 15:20:36 -070039EXPORT_SYMBOL(iomem_resource);
40
41static DEFINE_RWLOCK(resource_lock);
42
Bjorn Helgaase7f85672010-10-26 15:41:33 -060043/*
44 * By default, we allocate free space bottom-up. The architecture can request
45 * top-down by clearing this flag. The user can override the architecture's
46 * choice with the "resource_alloc_from_bottom" kernel boot option, but that
47 * should only be a debugging tool.
48 */
49int resource_alloc_from_bottom = 1;
50
51static __init int setup_alloc_from_bottom(char *s)
52{
53 printk(KERN_INFO
54 "resource: allocating from bottom-up; please report a bug\n");
55 resource_alloc_from_bottom = 1;
56 return 0;
57}
58early_param("resource_alloc_from_bottom", setup_alloc_from_bottom);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void *r_next(struct seq_file *m, void *v, loff_t *pos)
61{
62 struct resource *p = v;
63 (*pos)++;
64 if (p->child)
65 return p->child;
66 while (!p->sibling && p->parent)
67 p = p->parent;
68 return p->sibling;
69}
70
Ingo Molnar13eb8372008-09-26 10:10:12 +020071#ifdef CONFIG_PROC_FS
72
73enum { MAX_IORES_LEVEL = 5 };
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static void *r_start(struct seq_file *m, loff_t *pos)
76 __acquires(resource_lock)
77{
78 struct resource *p = m->private;
79 loff_t l = 0;
80 read_lock(&resource_lock);
81 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
82 ;
83 return p;
84}
85
86static void r_stop(struct seq_file *m, void *v)
87 __releases(resource_lock)
88{
89 read_unlock(&resource_lock);
90}
91
92static int r_show(struct seq_file *m, void *v)
93{
94 struct resource *root = m->private;
95 struct resource *r = v, *p;
96 int width = root->end < 0x10000 ? 4 : 8;
97 int depth;
98
99 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
100 if (p->parent == root)
101 break;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700102 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 depth * 2, "",
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700104 width, (unsigned long long) r->start,
105 width, (unsigned long long) r->end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 r->name ? r->name : "<BAD>");
107 return 0;
108}
109
Helge Deller15ad7cd2006-12-06 20:40:36 -0800110static const struct seq_operations resource_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .start = r_start,
112 .next = r_next,
113 .stop = r_stop,
114 .show = r_show,
115};
116
117static int ioports_open(struct inode *inode, struct file *file)
118{
119 int res = seq_open(file, &resource_op);
120 if (!res) {
121 struct seq_file *m = file->private_data;
122 m->private = &ioport_resource;
123 }
124 return res;
125}
126
127static int iomem_open(struct inode *inode, struct file *file)
128{
129 int res = seq_open(file, &resource_op);
130 if (!res) {
131 struct seq_file *m = file->private_data;
132 m->private = &iomem_resource;
133 }
134 return res;
135}
136
Helge Deller15ad7cd2006-12-06 20:40:36 -0800137static const struct file_operations proc_ioports_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .open = ioports_open,
139 .read = seq_read,
140 .llseek = seq_lseek,
141 .release = seq_release,
142};
143
Helge Deller15ad7cd2006-12-06 20:40:36 -0800144static const struct file_operations proc_iomem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .open = iomem_open,
146 .read = seq_read,
147 .llseek = seq_lseek,
148 .release = seq_release,
149};
150
151static int __init ioresources_init(void)
152{
Denis V. Lunevc33fff02008-04-29 01:02:31 -0700153 proc_create("ioports", 0, NULL, &proc_ioports_operations);
154 proc_create("iomem", 0, NULL, &proc_iomem_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156}
157__initcall(ioresources_init);
158
159#endif /* CONFIG_PROC_FS */
160
161/* Return the conflict entry if you can't request it */
162static struct resource * __request_resource(struct resource *root, struct resource *new)
163{
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700164 resource_size_t start = new->start;
165 resource_size_t end = new->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct resource *tmp, **p;
167
168 if (end < start)
169 return root;
170 if (start < root->start)
171 return root;
172 if (end > root->end)
173 return root;
174 p = &root->child;
175 for (;;) {
176 tmp = *p;
177 if (!tmp || tmp->start > end) {
178 new->sibling = tmp;
179 *p = new;
180 new->parent = root;
181 return NULL;
182 }
183 p = &tmp->sibling;
184 if (tmp->end < start)
185 continue;
186 return tmp;
187 }
188}
189
190static int __release_resource(struct resource *old)
191{
192 struct resource *tmp, **p;
193
194 p = &old->parent->child;
195 for (;;) {
196 tmp = *p;
197 if (!tmp)
198 break;
199 if (tmp == old) {
200 *p = tmp->sibling;
201 old->parent = NULL;
202 return 0;
203 }
204 p = &tmp->sibling;
205 }
206 return -EINVAL;
207}
208
Yinghai Lu5eeec0e2009-12-22 15:02:22 -0800209static void __release_child_resources(struct resource *r)
210{
211 struct resource *tmp, *p;
212 resource_size_t size;
213
214 p = r->child;
215 r->child = NULL;
216 while (p) {
217 tmp = p;
218 p = p->sibling;
219
220 tmp->parent = NULL;
221 tmp->sibling = NULL;
222 __release_child_resources(tmp);
223
224 printk(KERN_DEBUG "release child resource %pR\n", tmp);
225 /* need to restore size, and keep flags */
226 size = resource_size(tmp);
227 tmp->start = 0;
228 tmp->end = size - 1;
229 }
230}
231
232void release_child_resources(struct resource *r)
233{
234 write_lock(&resource_lock);
235 __release_child_resources(r);
236 write_unlock(&resource_lock);
237}
238
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700239/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700240 * request_resource_conflict - request and reserve an I/O or memory resource
241 * @root: root resource descriptor
242 * @new: resource descriptor desired by caller
243 *
244 * Returns 0 for success, conflict resource on error.
245 */
246struct resource *request_resource_conflict(struct resource *root, struct resource *new)
247{
248 struct resource *conflict;
249
250 write_lock(&resource_lock);
251 conflict = __request_resource(root, new);
252 write_unlock(&resource_lock);
253 return conflict;
254}
255
256/**
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700257 * request_resource - request and reserve an I/O or memory resource
258 * @root: root resource descriptor
259 * @new: resource descriptor desired by caller
260 *
261 * Returns 0 for success, negative error code on error.
262 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263int request_resource(struct resource *root, struct resource *new)
264{
265 struct resource *conflict;
266
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700267 conflict = request_resource_conflict(root, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return conflict ? -EBUSY : 0;
269}
270
271EXPORT_SYMBOL(request_resource);
272
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700273/**
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700274 * release_resource - release a previously reserved resource
275 * @old: resource pointer
276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277int release_resource(struct resource *old)
278{
279 int retval;
280
281 write_lock(&resource_lock);
282 retval = __release_resource(old);
283 write_unlock(&resource_lock);
284 return retval;
285}
286
287EXPORT_SYMBOL(release_resource);
288
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700289#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700290/*
291 * Finds the lowest memory reosurce exists within [res->start.res->end)
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700292 * the caller must specify res->start, res->end, res->flags and "name".
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700293 * If found, returns 0, res is overwritten, if not found, returns -1.
294 */
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700295static int find_next_system_ram(struct resource *res, char *name)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700296{
297 resource_size_t start, end;
298 struct resource *p;
299
300 BUG_ON(!res);
301
302 start = res->start;
303 end = res->end;
KAMEZAWA Hiroyuki58c1b5b2006-08-05 12:15:01 -0700304 BUG_ON(start >= end);
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700305
306 read_lock(&resource_lock);
307 for (p = iomem_resource.child; p ; p = p->sibling) {
308 /* system ram is just marked as IORESOURCE_MEM */
309 if (p->flags != res->flags)
310 continue;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700311 if (name && strcmp(p->name, name))
312 continue;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700313 if (p->start > end) {
314 p = NULL;
315 break;
316 }
KAMEZAWA Hiroyuki58c1b5b2006-08-05 12:15:01 -0700317 if ((p->end >= start) && (p->start < end))
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700318 break;
319 }
320 read_unlock(&resource_lock);
321 if (!p)
322 return -1;
323 /* copy data */
KAMEZAWA Hiroyuki0f04ab52006-08-05 12:14:59 -0700324 if (res->start < p->start)
325 res->start = p->start;
326 if (res->end > p->end)
327 res->end = p->end;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700328 return 0;
329}
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700330
331/*
332 * This function calls callback against all memory range of "System RAM"
333 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
334 * Now, this function is only for "System RAM".
335 */
336int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
337 void *arg, int (*func)(unsigned long, unsigned long, void *))
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700338{
339 struct resource res;
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800340 unsigned long pfn, end_pfn;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700341 u64 orig_end;
342 int ret = -1;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700343
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700344 res.start = (u64) start_pfn << PAGE_SHIFT;
345 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -0800346 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700347 orig_end = res.end;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700348 while ((res.start < res.end) &&
349 (find_next_system_ram(&res, "System RAM") >= 0)) {
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800350 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
351 end_pfn = (res.end + 1) >> PAGE_SHIFT;
352 if (end_pfn > pfn)
H. Peter Anvinf4149662010-03-02 11:21:09 -0800353 ret = (*func)(pfn, end_pfn - pfn, arg);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700354 if (ret)
355 break;
356 res.start = res.end + 1;
357 res.end = orig_end;
358 }
359 return ret;
360}
361
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700362#endif
363
Wu Fengguang61ef2482010-01-22 16:16:19 +0800364static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
365{
366 return 1;
367}
368/*
369 * This generic page_is_ram() returns true if specified address is
370 * registered as "System RAM" in iomem_resource list.
371 */
Andrew Mortone5273002010-01-26 16:31:19 -0800372int __weak page_is_ram(unsigned long pfn)
Wu Fengguang61ef2482010-01-22 16:16:19 +0800373{
374 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
375}
376
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600377static resource_size_t simple_align_resource(void *data,
378 const struct resource *avail,
379 resource_size_t size,
380 resource_size_t align)
381{
382 return avail->start;
383}
384
Bjorn Helgaas5d6b1fa2010-10-26 15:41:18 -0600385static void resource_clip(struct resource *res, resource_size_t min,
386 resource_size_t max)
387{
388 if (res->start < min)
389 res->start = min;
390 if (res->end > max)
391 res->end = max;
392}
393
Bjorn Helgaas6909ba12010-10-26 15:41:23 -0600394static bool resource_contains(struct resource *res1, struct resource *res2)
395{
396 return res1->start <= res2->start && res1->end >= res2->end;
397}
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/*
Bjorn Helgaase7f85672010-10-26 15:41:33 -0600400 * Find the resource before "child" in the sibling list of "root" children.
401 */
402static struct resource *find_sibling_prev(struct resource *root, struct resource *child)
403{
404 struct resource *this;
405
406 for (this = root->child; this; this = this->sibling)
407 if (this->sibling == child)
408 return this;
409
410 return NULL;
411}
412
413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * Find empty slot in the resource tree given range and alignment.
Bjorn Helgaase7f85672010-10-26 15:41:33 -0600415 * This version allocates from the end of the root resource first.
416 */
417static int find_resource_from_top(struct resource *root, struct resource *new,
418 resource_size_t size, resource_size_t min,
419 resource_size_t max, resource_size_t align,
420 resource_size_t (*alignf)(void *,
421 const struct resource *,
422 resource_size_t,
423 resource_size_t),
424 void *alignf_data)
425{
426 struct resource *this;
427 struct resource tmp, avail, alloc;
428
429 tmp.start = root->end;
430 tmp.end = root->end;
431
432 this = find_sibling_prev(root, NULL);
433 for (;;) {
434 if (this) {
435 if (this->end < root->end)
436 tmp.start = this->end + 1;
437 } else
438 tmp.start = root->start;
439
440 resource_clip(&tmp, min, max);
441
442 /* Check for overflow after ALIGN() */
443 avail = *new;
444 avail.start = ALIGN(tmp.start, align);
445 avail.end = tmp.end;
446 if (avail.start >= tmp.start) {
447 alloc.start = alignf(alignf_data, &avail, size, align);
448 alloc.end = alloc.start + size - 1;
449 if (resource_contains(&avail, &alloc)) {
450 new->start = alloc.start;
451 new->end = alloc.end;
452 return 0;
453 }
454 }
455
456 if (!this || this->start == root->start)
457 break;
458
459 tmp.end = this->start - 1;
460 this = find_sibling_prev(root, this);
461 }
462 return -EBUSY;
463}
464
465/*
466 * Find empty slot in the resource tree given range and alignment.
467 * This version allocates from the beginning of the root resource first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
469static int find_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700470 resource_size_t size, resource_size_t min,
471 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100472 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100473 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100474 resource_size_t,
475 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 void *alignf_data)
477{
478 struct resource *this = root->child;
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600479 struct resource tmp = *new, avail, alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100481 tmp.start = root->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /*
Bjorn Helgaase7f85672010-10-26 15:41:33 -0600483 * Skip past an allocated resource that starts at 0, since the
484 * assignment of this->start - 1 to tmp->end below would cause an
485 * underflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
487 if (this && this->start == 0) {
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100488 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 this = this->sibling;
490 }
Bjorn Helgaase7f85672010-10-26 15:41:33 -0600491 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (this)
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100493 tmp.end = this->start - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 else
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100495 tmp.end = root->end;
Bjorn Helgaas5d6b1fa2010-10-26 15:41:18 -0600496
497 resource_clip(&tmp, min, max);
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600498
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600499 /* Check for overflow after ALIGN() */
500 avail = *new;
501 avail.start = ALIGN(tmp.start, align);
502 avail.end = tmp.end;
503 if (avail.start >= tmp.start) {
504 alloc.start = alignf(alignf_data, &avail, size, align);
505 alloc.end = alloc.start + size - 1;
506 if (resource_contains(&avail, &alloc)) {
507 new->start = alloc.start;
508 new->end = alloc.end;
509 return 0;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Bjorn Helgaase7f85672010-10-26 15:41:33 -0600512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!this)
514 break;
Bjorn Helgaase7f85672010-10-26 15:41:33 -0600515
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100516 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 this = this->sibling;
518 }
519 return -EBUSY;
520}
521
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700522/**
523 * allocate_resource - allocate empty slot in the resource tree given range & alignment
524 * @root: root resource descriptor
525 * @new: resource descriptor desired by caller
526 * @size: requested resource region size
527 * @min: minimum size to allocate
528 * @max: maximum size to allocate
529 * @align: alignment requested, in bytes
530 * @alignf: alignment function, optional, called if not NULL
531 * @alignf_data: arbitrary data to pass to the @alignf function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
533int allocate_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700534 resource_size_t size, resource_size_t min,
535 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100536 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100537 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100538 resource_size_t,
539 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 void *alignf_data)
541{
542 int err;
543
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600544 if (!alignf)
545 alignf = simple_align_resource;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 write_lock(&resource_lock);
Bjorn Helgaase7f85672010-10-26 15:41:33 -0600548 if (resource_alloc_from_bottom)
549 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
550 else
551 err = find_resource_from_top(root, new, size, min, max, align, alignf, alignf_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (err >= 0 && __request_resource(root, new))
553 err = -EBUSY;
554 write_unlock(&resource_lock);
555 return err;
556}
557
558EXPORT_SYMBOL(allocate_resource);
559
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700560/*
561 * Insert a resource into the resource tree. If successful, return NULL,
562 * otherwise return the conflicting resource (compare to __request_resource())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700564static struct resource * __insert_resource(struct resource *parent, struct resource *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct resource *first, *next;
567
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700568 for (;; parent = first) {
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700569 first = __request_resource(parent, new);
570 if (!first)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700571 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700573 if (first == parent)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700574 return first;
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700575
576 if ((first->start > new->start) || (first->end < new->end))
577 break;
578 if ((first->start == new->start) && (first->end == new->end))
579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 for (next = first; ; next = next->sibling) {
583 /* Partial overlap? Bad, and unfixable */
584 if (next->start < new->start || next->end > new->end)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700585 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!next->sibling)
587 break;
588 if (next->sibling->start > new->end)
589 break;
590 }
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 new->parent = parent;
593 new->sibling = next->sibling;
594 new->child = first;
595
596 next->sibling = NULL;
597 for (next = first; next; next = next->sibling)
598 next->parent = new;
599
600 if (parent->child == first) {
601 parent->child = new;
602 } else {
603 next = parent->child;
604 while (next->sibling != first)
605 next = next->sibling;
606 next->sibling = new;
607 }
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700608 return NULL;
609}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700611/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700612 * insert_resource_conflict - Inserts resource in the resource tree
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700613 * @parent: parent of the new resource
614 * @new: new resource to insert
615 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700616 * Returns 0 on success, conflict resource if the resource can't be inserted.
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700617 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700618 * This function is equivalent to request_resource_conflict when no conflict
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700619 * happens. If a conflict happens, and the conflicting resources
620 * entirely fit within the range of the new resource, then the new
621 * resource is inserted and the conflicting resources become children of
622 * the new resource.
623 */
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700624struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700625{
626 struct resource *conflict;
627
628 write_lock(&resource_lock);
629 conflict = __insert_resource(parent, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 write_unlock(&resource_lock);
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700631 return conflict;
632}
633
634/**
635 * insert_resource - Inserts a resource in the resource tree
636 * @parent: parent of the new resource
637 * @new: new resource to insert
638 *
639 * Returns 0 on success, -EBUSY if the resource can't be inserted.
640 */
641int insert_resource(struct resource *parent, struct resource *new)
642{
643 struct resource *conflict;
644
645 conflict = insert_resource_conflict(parent, new);
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700646 return conflict ? -EBUSY : 0;
647}
648
649/**
650 * insert_resource_expand_to_fit - Insert a resource into the resource tree
Randy Dunlap6781f4a2008-08-31 20:31:55 -0700651 * @root: root resource descriptor
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700652 * @new: new resource to insert
653 *
654 * Insert a resource into the resource tree, possibly expanding it in order
655 * to make it encompass any conflicting resources.
656 */
657void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
658{
659 if (new->parent)
660 return;
661
662 write_lock(&resource_lock);
663 for (;;) {
664 struct resource *conflict;
665
666 conflict = __insert_resource(root, new);
667 if (!conflict)
668 break;
669 if (conflict == root)
670 break;
671
672 /* Ok, expand resource to cover the conflict, then try again .. */
673 if (conflict->start < new->start)
674 new->start = conflict->start;
675 if (conflict->end > new->end)
676 new->end = conflict->end;
677
678 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
679 }
680 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700683/**
684 * adjust_resource - modify a resource's start and size
685 * @res: resource to modify
686 * @start: new start value
687 * @size: new size
688 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * Given an existing resource, change its start and size to match the
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700690 * arguments. Returns 0 on success, -EBUSY if it can't fit.
691 * Existing children of the resource are assumed to be immutable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700693int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 struct resource *tmp, *parent = res->parent;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700696 resource_size_t end = start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int result = -EBUSY;
698
699 write_lock(&resource_lock);
700
701 if ((start < parent->start) || (end > parent->end))
702 goto out;
703
704 for (tmp = res->child; tmp; tmp = tmp->sibling) {
705 if ((tmp->start < start) || (tmp->end > end))
706 goto out;
707 }
708
709 if (res->sibling && (res->sibling->start <= end))
710 goto out;
711
712 tmp = parent->child;
713 if (tmp != res) {
714 while (tmp->sibling != res)
715 tmp = tmp->sibling;
716 if (start <= tmp->end)
717 goto out;
718 }
719
720 res->start = start;
721 res->end = end;
722 result = 0;
723
724 out:
725 write_unlock(&resource_lock);
726 return result;
727}
728
Yinghai Lu268364a2008-09-04 21:02:44 +0200729static void __init __reserve_region_with_split(struct resource *root,
730 resource_size_t start, resource_size_t end,
731 const char *name)
732{
733 struct resource *parent = root;
734 struct resource *conflict;
Linus Torvalds42c02022008-11-01 09:53:58 -0700735 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
Yinghai Lu268364a2008-09-04 21:02:44 +0200736
737 if (!res)
738 return;
739
740 res->name = name;
741 res->start = start;
742 res->end = end;
743 res->flags = IORESOURCE_BUSY;
744
Linus Torvaldsff542502009-04-18 21:44:24 -0700745 conflict = __request_resource(parent, res);
746 if (!conflict)
747 return;
Yinghai Lu268364a2008-09-04 21:02:44 +0200748
Linus Torvaldsff542502009-04-18 21:44:24 -0700749 /* failed, split and try again */
750 kfree(res);
Yinghai Lu268364a2008-09-04 21:02:44 +0200751
Linus Torvaldsff542502009-04-18 21:44:24 -0700752 /* conflict covered whole area */
753 if (conflict->start <= start && conflict->end >= end)
754 return;
Yinghai Lu268364a2008-09-04 21:02:44 +0200755
Linus Torvaldsff542502009-04-18 21:44:24 -0700756 if (conflict->start > start)
757 __reserve_region_with_split(root, start, conflict->start-1, name);
758 if (conflict->end < end)
759 __reserve_region_with_split(root, conflict->end+1, end, name);
Yinghai Lu268364a2008-09-04 21:02:44 +0200760}
761
Paul Mundtbea92112008-10-22 19:31:11 +0900762void __init reserve_region_with_split(struct resource *root,
Yinghai Lu268364a2008-09-04 21:02:44 +0200763 resource_size_t start, resource_size_t end,
764 const char *name)
765{
766 write_lock(&resource_lock);
767 __reserve_region_with_split(root, start, end, name);
768 write_unlock(&resource_lock);
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771EXPORT_SYMBOL(adjust_resource);
772
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400773/**
774 * resource_alignment - calculate resource's alignment
775 * @res: resource pointer
776 *
777 * Returns alignment on success, 0 (invalid alignment) on failure.
778 */
779resource_size_t resource_alignment(struct resource *res)
780{
781 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
782 case IORESOURCE_SIZEALIGN:
Magnus Damm1a4e5642008-07-29 22:32:57 -0700783 return resource_size(res);
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400784 case IORESOURCE_STARTALIGN:
785 return res->start;
786 default:
787 return 0;
788 }
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
792 * This is compatibility stuff for IO resources.
793 *
794 * Note how this, unlike the above, knows about
795 * the IO flag meanings (busy etc).
796 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700797 * request_region creates a new busy region.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700799 * check_region returns non-zero if the area is already busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700801 * release_region releases a matching busy region.
802 */
803
Alan Cox8b6d0432010-03-29 19:38:00 +0200804static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
805
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700806/**
807 * __request_region - create a new busy resource region
808 * @parent: parent resource descriptor
809 * @start: resource start address
810 * @n: resource region size
811 * @name: reserving caller's ID string
Randy Dunlap6ae301e2009-01-15 13:51:01 -0800812 * @flags: IO resource flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700814struct resource * __request_region(struct resource *parent,
815 resource_size_t start, resource_size_t n,
Arjan van de Vene8de1482008-10-22 19:55:31 -0700816 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Alan Cox8b6d0432010-03-29 19:38:00 +0200818 DECLARE_WAITQUEUE(wait, current);
Pekka J Enbergdd392712005-09-06 15:18:31 -0700819 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700821 if (!res)
822 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700824 res->name = name;
825 res->start = start;
826 res->end = start + n - 1;
827 res->flags = IORESOURCE_BUSY;
Arjan van de Vene8de1482008-10-22 19:55:31 -0700828 res->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700830 write_lock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700832 for (;;) {
833 struct resource *conflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700835 conflict = __request_resource(parent, res);
836 if (!conflict)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700838 if (conflict != parent) {
839 parent = conflict;
840 if (!(conflict->flags & IORESOURCE_BUSY))
841 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Alan Cox8b6d0432010-03-29 19:38:00 +0200843 if (conflict->flags & flags & IORESOURCE_MUXED) {
844 add_wait_queue(&muxed_resource_wait, &wait);
845 write_unlock(&resource_lock);
846 set_current_state(TASK_UNINTERRUPTIBLE);
847 schedule();
848 remove_wait_queue(&muxed_resource_wait, &wait);
849 write_lock(&resource_lock);
850 continue;
851 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700852 /* Uhhuh, that didn't work out.. */
853 kfree(res);
854 res = NULL;
855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700857 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return res;
859}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860EXPORT_SYMBOL(__request_region);
861
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700862/**
863 * __check_region - check if a resource region is busy or free
864 * @parent: parent resource descriptor
865 * @start: resource start address
866 * @n: resource region size
867 *
868 * Returns 0 if the region is free at the moment it is checked,
869 * returns %-EBUSY if the region is busy.
870 *
871 * NOTE:
872 * This function is deprecated because its use is racy.
873 * Even if it returns 0, a subsequent call to request_region()
874 * may fail because another driver etc. just allocated the region.
875 * Do NOT use it. It will be removed from the kernel.
876 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700877int __check_region(struct resource *parent, resource_size_t start,
878 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct resource * res;
881
Arjan van de Vene8de1482008-10-22 19:55:31 -0700882 res = __request_region(parent, start, n, "check-region", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (!res)
884 return -EBUSY;
885
886 release_resource(res);
887 kfree(res);
888 return 0;
889}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890EXPORT_SYMBOL(__check_region);
891
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700892/**
893 * __release_region - release a previously reserved resource region
894 * @parent: parent resource descriptor
895 * @start: resource start address
896 * @n: resource region size
897 *
898 * The described resource region must match a currently busy region.
899 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700900void __release_region(struct resource *parent, resource_size_t start,
901 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct resource **p;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700904 resource_size_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 p = &parent->child;
907 end = start + n - 1;
908
909 write_lock(&resource_lock);
910
911 for (;;) {
912 struct resource *res = *p;
913
914 if (!res)
915 break;
916 if (res->start <= start && res->end >= end) {
917 if (!(res->flags & IORESOURCE_BUSY)) {
918 p = &res->child;
919 continue;
920 }
921 if (res->start != start || res->end != end)
922 break;
923 *p = res->sibling;
924 write_unlock(&resource_lock);
Alan Cox8b6d0432010-03-29 19:38:00 +0200925 if (res->flags & IORESOURCE_MUXED)
926 wake_up(&muxed_resource_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 kfree(res);
928 return;
929 }
930 p = &res->sibling;
931 }
932
933 write_unlock(&resource_lock);
934
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700935 printk(KERN_WARNING "Trying to free nonexistent resource "
936 "<%016llx-%016llx>\n", (unsigned long long)start,
937 (unsigned long long)end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939EXPORT_SYMBOL(__release_region);
940
941/*
Tejun Heo9ac78492007-01-20 16:00:26 +0900942 * Managed region resource
943 */
944struct region_devres {
945 struct resource *parent;
946 resource_size_t start;
947 resource_size_t n;
948};
949
950static void devm_region_release(struct device *dev, void *res)
951{
952 struct region_devres *this = res;
953
954 __release_region(this->parent, this->start, this->n);
955}
956
957static int devm_region_match(struct device *dev, void *res, void *match_data)
958{
959 struct region_devres *this = res, *match = match_data;
960
961 return this->parent == match->parent &&
962 this->start == match->start && this->n == match->n;
963}
964
965struct resource * __devm_request_region(struct device *dev,
966 struct resource *parent, resource_size_t start,
967 resource_size_t n, const char *name)
968{
969 struct region_devres *dr = NULL;
970 struct resource *res;
971
972 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
973 GFP_KERNEL);
974 if (!dr)
975 return NULL;
976
977 dr->parent = parent;
978 dr->start = start;
979 dr->n = n;
980
Arjan van de Vene8de1482008-10-22 19:55:31 -0700981 res = __request_region(parent, start, n, name, 0);
Tejun Heo9ac78492007-01-20 16:00:26 +0900982 if (res)
983 devres_add(dev, dr);
984 else
985 devres_free(dr);
986
987 return res;
988}
989EXPORT_SYMBOL(__devm_request_region);
990
991void __devm_release_region(struct device *dev, struct resource *parent,
992 resource_size_t start, resource_size_t n)
993{
994 struct region_devres match_data = { parent, start, n };
995
996 __release_region(parent, start, n);
997 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
998 &match_data));
999}
1000EXPORT_SYMBOL(__devm_release_region);
1001
1002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 * Called from init/main.c to reserve IO ports.
1004 */
1005#define MAXRESERVE 4
1006static int __init reserve_setup(char *str)
1007{
1008 static int reserved;
1009 static struct resource reserve[MAXRESERVE];
1010
1011 for (;;) {
Zhang Rui8bc1ad72009-06-30 11:41:31 -07001012 unsigned int io_start, io_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int x = reserved;
1014
1015 if (get_option (&str, &io_start) != 2)
1016 break;
1017 if (get_option (&str, &io_num) == 0)
1018 break;
1019 if (x < MAXRESERVE) {
1020 struct resource *res = reserve + x;
1021 res->name = "reserved";
1022 res->start = io_start;
1023 res->end = io_start + io_num - 1;
1024 res->flags = IORESOURCE_BUSY;
1025 res->child = NULL;
1026 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
1027 reserved = x+1;
1028 }
1029 }
1030 return 1;
1031}
1032
1033__setup("reserve=", reserve_setup);
Suresh Siddha379daf62008-09-25 18:43:34 -07001034
1035/*
1036 * Check if the requested addr and size spans more than any slot in the
1037 * iomem resource tree.
1038 */
1039int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1040{
1041 struct resource *p = &iomem_resource;
1042 int err = 0;
1043 loff_t l;
1044
1045 read_lock(&resource_lock);
1046 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1047 /*
1048 * We can probably skip the resources without
1049 * IORESOURCE_IO attribute?
1050 */
1051 if (p->start >= addr + size)
1052 continue;
1053 if (p->end < addr)
1054 continue;
Suresh Siddhad68612b2008-10-28 11:45:42 -07001055 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1056 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
Suresh Siddha379daf62008-09-25 18:43:34 -07001057 continue;
Arjan van de Ven3ac52662008-12-13 09:15:27 -08001058 /*
1059 * if a resource is "BUSY", it's not a hardware resource
1060 * but a driver mapping of such a resource; we don't want
1061 * to warn for those; some drivers legitimately map only
1062 * partial hardware resources. (example: vesafb)
1063 */
1064 if (p->flags & IORESOURCE_BUSY)
1065 continue;
1066
Suresh Siddha379daf62008-09-25 18:43:34 -07001067 printk(KERN_WARNING "resource map sanity check conflict: "
1068 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
Ingo Molnar13eb8372008-09-26 10:10:12 +02001069 (unsigned long long)addr,
1070 (unsigned long long)(addr + size - 1),
1071 (unsigned long long)p->start,
1072 (unsigned long long)p->end,
1073 p->name);
Suresh Siddha379daf62008-09-25 18:43:34 -07001074 err = -1;
1075 break;
1076 }
1077 read_unlock(&resource_lock);
1078
1079 return err;
1080}
Arjan van de Vene8de1482008-10-22 19:55:31 -07001081
1082#ifdef CONFIG_STRICT_DEVMEM
1083static int strict_iomem_checks = 1;
1084#else
1085static int strict_iomem_checks;
1086#endif
1087
1088/*
1089 * check if an address is reserved in the iomem resource tree
1090 * returns 1 if reserved, 0 if not reserved.
1091 */
1092int iomem_is_exclusive(u64 addr)
1093{
1094 struct resource *p = &iomem_resource;
1095 int err = 0;
1096 loff_t l;
1097 int size = PAGE_SIZE;
1098
1099 if (!strict_iomem_checks)
1100 return 0;
1101
1102 addr = addr & PAGE_MASK;
1103
1104 read_lock(&resource_lock);
1105 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1106 /*
1107 * We can probably skip the resources without
1108 * IORESOURCE_IO attribute?
1109 */
1110 if (p->start >= addr + size)
1111 break;
1112 if (p->end < addr)
1113 continue;
1114 if (p->flags & IORESOURCE_BUSY &&
1115 p->flags & IORESOURCE_EXCLUSIVE) {
1116 err = 1;
1117 break;
1118 }
1119 }
1120 read_unlock(&resource_lock);
1121
1122 return err;
1123}
1124
1125static int __init strict_iomem(char *str)
1126{
1127 if (strstr(str, "relaxed"))
1128 strict_iomem_checks = 0;
1129 if (strstr(str, "strict"))
1130 strict_iomem_checks = 1;
1131 return 1;
1132}
1133
1134__setup("iomem=", strict_iomem);