blob: 9c9841cb69021fdd259510ce7f117cedf14f0ad7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static void *r_next(struct seq_file *m, void *v, loff_t *pos)
44{
45 struct resource *p = v;
46 (*pos)++;
47 if (p->child)
48 return p->child;
49 while (!p->sibling && p->parent)
50 p = p->parent;
51 return p->sibling;
52}
53
Ingo Molnar13eb8372008-09-26 10:10:12 +020054#ifdef CONFIG_PROC_FS
55
56enum { MAX_IORES_LEVEL = 5 };
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void *r_start(struct seq_file *m, loff_t *pos)
59 __acquires(resource_lock)
60{
61 struct resource *p = m->private;
62 loff_t l = 0;
63 read_lock(&resource_lock);
64 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
65 ;
66 return p;
67}
68
69static void r_stop(struct seq_file *m, void *v)
70 __releases(resource_lock)
71{
72 read_unlock(&resource_lock);
73}
74
75static int r_show(struct seq_file *m, void *v)
76{
77 struct resource *root = m->private;
78 struct resource *r = v, *p;
79 int width = root->end < 0x10000 ? 4 : 8;
80 int depth;
81
82 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
83 if (p->parent == root)
84 break;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -070085 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 depth * 2, "",
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -070087 width, (unsigned long long) r->start,
88 width, (unsigned long long) r->end,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 r->name ? r->name : "<BAD>");
90 return 0;
91}
92
Helge Deller15ad7cd2006-12-06 20:40:36 -080093static const struct seq_operations resource_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .start = r_start,
95 .next = r_next,
96 .stop = r_stop,
97 .show = r_show,
98};
99
100static int ioports_open(struct inode *inode, struct file *file)
101{
102 int res = seq_open(file, &resource_op);
103 if (!res) {
104 struct seq_file *m = file->private_data;
105 m->private = &ioport_resource;
106 }
107 return res;
108}
109
110static int iomem_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 = &iomem_resource;
116 }
117 return res;
118}
119
Helge Deller15ad7cd2006-12-06 20:40:36 -0800120static const struct file_operations proc_ioports_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .open = ioports_open,
122 .read = seq_read,
123 .llseek = seq_lseek,
124 .release = seq_release,
125};
126
Helge Deller15ad7cd2006-12-06 20:40:36 -0800127static const struct file_operations proc_iomem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .open = iomem_open,
129 .read = seq_read,
130 .llseek = seq_lseek,
131 .release = seq_release,
132};
133
134static int __init ioresources_init(void)
135{
Denis V. Lunevc33fff02008-04-29 01:02:31 -0700136 proc_create("ioports", 0, NULL, &proc_ioports_operations);
137 proc_create("iomem", 0, NULL, &proc_iomem_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140__initcall(ioresources_init);
141
142#endif /* CONFIG_PROC_FS */
143
144/* Return the conflict entry if you can't request it */
145static struct resource * __request_resource(struct resource *root, struct resource *new)
146{
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700147 resource_size_t start = new->start;
148 resource_size_t end = new->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct resource *tmp, **p;
150
151 if (end < start)
152 return root;
153 if (start < root->start)
154 return root;
155 if (end > root->end)
156 return root;
157 p = &root->child;
158 for (;;) {
159 tmp = *p;
160 if (!tmp || tmp->start > end) {
161 new->sibling = tmp;
162 *p = new;
163 new->parent = root;
164 return NULL;
165 }
166 p = &tmp->sibling;
167 if (tmp->end < start)
168 continue;
169 return tmp;
170 }
171}
172
173static int __release_resource(struct resource *old)
174{
175 struct resource *tmp, **p;
176
177 p = &old->parent->child;
178 for (;;) {
179 tmp = *p;
180 if (!tmp)
181 break;
182 if (tmp == old) {
183 *p = tmp->sibling;
184 old->parent = NULL;
185 return 0;
186 }
187 p = &tmp->sibling;
188 }
189 return -EINVAL;
190}
191
Yinghai Lu5eeec0e2009-12-22 15:02:22 -0800192static void __release_child_resources(struct resource *r)
193{
194 struct resource *tmp, *p;
195 resource_size_t size;
196
197 p = r->child;
198 r->child = NULL;
199 while (p) {
200 tmp = p;
201 p = p->sibling;
202
203 tmp->parent = NULL;
204 tmp->sibling = NULL;
205 __release_child_resources(tmp);
206
207 printk(KERN_DEBUG "release child resource %pR\n", tmp);
208 /* need to restore size, and keep flags */
209 size = resource_size(tmp);
210 tmp->start = 0;
211 tmp->end = size - 1;
212 }
213}
214
215void release_child_resources(struct resource *r)
216{
217 write_lock(&resource_lock);
218 __release_child_resources(r);
219 write_unlock(&resource_lock);
220}
221
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700222/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700223 * request_resource_conflict - request and reserve an I/O or memory resource
224 * @root: root resource descriptor
225 * @new: resource descriptor desired by caller
226 *
227 * Returns 0 for success, conflict resource on error.
228 */
229struct resource *request_resource_conflict(struct resource *root, struct resource *new)
230{
231 struct resource *conflict;
232
233 write_lock(&resource_lock);
234 conflict = __request_resource(root, new);
235 write_unlock(&resource_lock);
236 return conflict;
237}
238
239/**
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700240 * request_resource - 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, negative error code on error.
245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246int request_resource(struct resource *root, struct resource *new)
247{
248 struct resource *conflict;
249
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700250 conflict = request_resource_conflict(root, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return conflict ? -EBUSY : 0;
252}
253
254EXPORT_SYMBOL(request_resource);
255
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700256/**
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700257 * release_resource - release a previously reserved resource
258 * @old: resource pointer
259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260int release_resource(struct resource *old)
261{
262 int retval;
263
264 write_lock(&resource_lock);
265 retval = __release_resource(old);
266 write_unlock(&resource_lock);
267 return retval;
268}
269
270EXPORT_SYMBOL(release_resource);
271
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700272#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700273/*
274 * Finds the lowest memory reosurce exists within [res->start.res->end)
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700275 * the caller must specify res->start, res->end, res->flags and "name".
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700276 * If found, returns 0, res is overwritten, if not found, returns -1.
277 */
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700278static int find_next_system_ram(struct resource *res, char *name)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700279{
280 resource_size_t start, end;
281 struct resource *p;
282
283 BUG_ON(!res);
284
285 start = res->start;
286 end = res->end;
KAMEZAWA Hiroyuki58c1b5b2006-08-05 12:15:01 -0700287 BUG_ON(start >= end);
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700288
289 read_lock(&resource_lock);
290 for (p = iomem_resource.child; p ; p = p->sibling) {
291 /* system ram is just marked as IORESOURCE_MEM */
292 if (p->flags != res->flags)
293 continue;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700294 if (name && strcmp(p->name, name))
295 continue;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700296 if (p->start > end) {
297 p = NULL;
298 break;
299 }
KAMEZAWA Hiroyuki58c1b5b2006-08-05 12:15:01 -0700300 if ((p->end >= start) && (p->start < end))
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700301 break;
302 }
303 read_unlock(&resource_lock);
304 if (!p)
305 return -1;
306 /* copy data */
KAMEZAWA Hiroyuki0f04ab52006-08-05 12:14:59 -0700307 if (res->start < p->start)
308 res->start = p->start;
309 if (res->end > p->end)
310 res->end = p->end;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700311 return 0;
312}
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700313
314/*
315 * This function calls callback against all memory range of "System RAM"
316 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
317 * Now, this function is only for "System RAM".
318 */
319int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
320 void *arg, int (*func)(unsigned long, unsigned long, void *))
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700321{
322 struct resource res;
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800323 unsigned long pfn, end_pfn;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700324 u64 orig_end;
325 int ret = -1;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700326
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700327 res.start = (u64) start_pfn << PAGE_SHIFT;
328 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
Yasunori Goto887c3cb2007-11-14 16:59:20 -0800329 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700330 orig_end = res.end;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700331 while ((res.start < res.end) &&
332 (find_next_system_ram(&res, "System RAM") >= 0)) {
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800333 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
334 end_pfn = (res.end + 1) >> PAGE_SHIFT;
335 if (end_pfn > pfn)
H. Peter Anvinf4149662010-03-02 11:21:09 -0800336 ret = (*func)(pfn, end_pfn - pfn, arg);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700337 if (ret)
338 break;
339 res.start = res.end + 1;
340 res.end = orig_end;
341 }
342 return ret;
343}
344
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700345#endif
346
Wu Fengguang61ef2482010-01-22 16:16:19 +0800347static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
348{
349 return 1;
350}
351/*
352 * This generic page_is_ram() returns true if specified address is
353 * registered as "System RAM" in iomem_resource list.
354 */
Andrew Mortone5273002010-01-26 16:31:19 -0800355int __weak page_is_ram(unsigned long pfn)
Wu Fengguang61ef2482010-01-22 16:16:19 +0800356{
357 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * Find empty slot in the resource tree given range and alignment.
362 */
363static int find_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700364 resource_size_t size, resource_size_t min,
365 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100366 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100367 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100368 resource_size_t,
369 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 void *alignf_data)
371{
372 struct resource *this = root->child;
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100373 struct resource tmp = *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100375 tmp.start = root->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /*
377 * Skip past an allocated resource that starts at 0, since the assignment
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100378 * of this->start - 1 to tmp->end below would cause an underflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
380 if (this && this->start == 0) {
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100381 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 this = this->sibling;
383 }
384 for(;;) {
385 if (this)
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100386 tmp.end = this->start - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 else
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100388 tmp.end = root->end;
389 if (tmp.start < min)
390 tmp.start = min;
391 if (tmp.end > max)
392 tmp.end = max;
393 tmp.start = ALIGN(tmp.start, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (alignf)
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100395 tmp.start = alignf(alignf_data, &tmp, size, align);
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100396 if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
397 new->start = tmp.start;
398 new->end = tmp.start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0;
400 }
401 if (!this)
402 break;
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100403 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 this = this->sibling;
405 }
406 return -EBUSY;
407}
408
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700409/**
410 * allocate_resource - allocate empty slot in the resource tree given range & alignment
411 * @root: root resource descriptor
412 * @new: resource descriptor desired by caller
413 * @size: requested resource region size
414 * @min: minimum size to allocate
415 * @max: maximum size to allocate
416 * @align: alignment requested, in bytes
417 * @alignf: alignment function, optional, called if not NULL
418 * @alignf_data: arbitrary data to pass to the @alignf function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
420int allocate_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700421 resource_size_t size, resource_size_t min,
422 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100423 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100424 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100425 resource_size_t,
426 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 void *alignf_data)
428{
429 int err;
430
431 write_lock(&resource_lock);
432 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
433 if (err >= 0 && __request_resource(root, new))
434 err = -EBUSY;
435 write_unlock(&resource_lock);
436 return err;
437}
438
439EXPORT_SYMBOL(allocate_resource);
440
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700441/*
442 * Insert a resource into the resource tree. If successful, return NULL,
443 * otherwise return the conflicting resource (compare to __request_resource())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700445static struct resource * __insert_resource(struct resource *parent, struct resource *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct resource *first, *next;
448
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700449 for (;; parent = first) {
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700450 first = __request_resource(parent, new);
451 if (!first)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700452 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700454 if (first == parent)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700455 return first;
Huang Shijie5de1cb22010-10-27 15:34:52 -0700456 if (WARN_ON(first == new)) /* duplicated insertion */
457 return first;
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700458
459 if ((first->start > new->start) || (first->end < new->end))
460 break;
461 if ((first->start == new->start) && (first->end == new->end))
462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
465 for (next = first; ; next = next->sibling) {
466 /* Partial overlap? Bad, and unfixable */
467 if (next->start < new->start || next->end > new->end)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700468 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!next->sibling)
470 break;
471 if (next->sibling->start > new->end)
472 break;
473 }
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 new->parent = parent;
476 new->sibling = next->sibling;
477 new->child = first;
478
479 next->sibling = NULL;
480 for (next = first; next; next = next->sibling)
481 next->parent = new;
482
483 if (parent->child == first) {
484 parent->child = new;
485 } else {
486 next = parent->child;
487 while (next->sibling != first)
488 next = next->sibling;
489 next->sibling = new;
490 }
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700491 return NULL;
492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700494/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700495 * insert_resource_conflict - Inserts resource in the resource tree
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700496 * @parent: parent of the new resource
497 * @new: new resource to insert
498 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700499 * Returns 0 on success, conflict resource if the resource can't be inserted.
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700500 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700501 * This function is equivalent to request_resource_conflict when no conflict
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700502 * happens. If a conflict happens, and the conflicting resources
503 * entirely fit within the range of the new resource, then the new
504 * resource is inserted and the conflicting resources become children of
505 * the new resource.
506 */
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700507struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700508{
509 struct resource *conflict;
510
511 write_lock(&resource_lock);
512 conflict = __insert_resource(parent, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 write_unlock(&resource_lock);
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700514 return conflict;
515}
516
517/**
518 * insert_resource - Inserts a resource in the resource tree
519 * @parent: parent of the new resource
520 * @new: new resource to insert
521 *
522 * Returns 0 on success, -EBUSY if the resource can't be inserted.
523 */
524int insert_resource(struct resource *parent, struct resource *new)
525{
526 struct resource *conflict;
527
528 conflict = insert_resource_conflict(parent, new);
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700529 return conflict ? -EBUSY : 0;
530}
531
532/**
533 * insert_resource_expand_to_fit - Insert a resource into the resource tree
Randy Dunlap6781f4a2008-08-31 20:31:55 -0700534 * @root: root resource descriptor
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700535 * @new: new resource to insert
536 *
537 * Insert a resource into the resource tree, possibly expanding it in order
538 * to make it encompass any conflicting resources.
539 */
540void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
541{
542 if (new->parent)
543 return;
544
545 write_lock(&resource_lock);
546 for (;;) {
547 struct resource *conflict;
548
549 conflict = __insert_resource(root, new);
550 if (!conflict)
551 break;
552 if (conflict == root)
553 break;
554
555 /* Ok, expand resource to cover the conflict, then try again .. */
556 if (conflict->start < new->start)
557 new->start = conflict->start;
558 if (conflict->end > new->end)
559 new->end = conflict->end;
560
561 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
562 }
563 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700566/**
567 * adjust_resource - modify a resource's start and size
568 * @res: resource to modify
569 * @start: new start value
570 * @size: new size
571 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * Given an existing resource, change its start and size to match the
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700573 * arguments. Returns 0 on success, -EBUSY if it can't fit.
574 * Existing children of the resource are assumed to be immutable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700576int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct resource *tmp, *parent = res->parent;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700579 resource_size_t end = start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int result = -EBUSY;
581
582 write_lock(&resource_lock);
583
584 if ((start < parent->start) || (end > parent->end))
585 goto out;
586
587 for (tmp = res->child; tmp; tmp = tmp->sibling) {
588 if ((tmp->start < start) || (tmp->end > end))
589 goto out;
590 }
591
592 if (res->sibling && (res->sibling->start <= end))
593 goto out;
594
595 tmp = parent->child;
596 if (tmp != res) {
597 while (tmp->sibling != res)
598 tmp = tmp->sibling;
599 if (start <= tmp->end)
600 goto out;
601 }
602
603 res->start = start;
604 res->end = end;
605 result = 0;
606
607 out:
608 write_unlock(&resource_lock);
609 return result;
610}
611
Yinghai Lu268364a2008-09-04 21:02:44 +0200612static void __init __reserve_region_with_split(struct resource *root,
613 resource_size_t start, resource_size_t end,
614 const char *name)
615{
616 struct resource *parent = root;
617 struct resource *conflict;
Linus Torvalds42c02022008-11-01 09:53:58 -0700618 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
Yinghai Lu268364a2008-09-04 21:02:44 +0200619
620 if (!res)
621 return;
622
623 res->name = name;
624 res->start = start;
625 res->end = end;
626 res->flags = IORESOURCE_BUSY;
627
Linus Torvaldsff542502009-04-18 21:44:24 -0700628 conflict = __request_resource(parent, res);
629 if (!conflict)
630 return;
Yinghai Lu268364a2008-09-04 21:02:44 +0200631
Linus Torvaldsff542502009-04-18 21:44:24 -0700632 /* failed, split and try again */
633 kfree(res);
Yinghai Lu268364a2008-09-04 21:02:44 +0200634
Linus Torvaldsff542502009-04-18 21:44:24 -0700635 /* conflict covered whole area */
636 if (conflict->start <= start && conflict->end >= end)
637 return;
Yinghai Lu268364a2008-09-04 21:02:44 +0200638
Linus Torvaldsff542502009-04-18 21:44:24 -0700639 if (conflict->start > start)
640 __reserve_region_with_split(root, start, conflict->start-1, name);
641 if (conflict->end < end)
642 __reserve_region_with_split(root, conflict->end+1, end, name);
Yinghai Lu268364a2008-09-04 21:02:44 +0200643}
644
Paul Mundtbea92112008-10-22 19:31:11 +0900645void __init reserve_region_with_split(struct resource *root,
Yinghai Lu268364a2008-09-04 21:02:44 +0200646 resource_size_t start, resource_size_t end,
647 const char *name)
648{
649 write_lock(&resource_lock);
650 __reserve_region_with_split(root, start, end, name);
651 write_unlock(&resource_lock);
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654EXPORT_SYMBOL(adjust_resource);
655
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400656/**
657 * resource_alignment - calculate resource's alignment
658 * @res: resource pointer
659 *
660 * Returns alignment on success, 0 (invalid alignment) on failure.
661 */
662resource_size_t resource_alignment(struct resource *res)
663{
664 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
665 case IORESOURCE_SIZEALIGN:
Magnus Damm1a4e5642008-07-29 22:32:57 -0700666 return resource_size(res);
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400667 case IORESOURCE_STARTALIGN:
668 return res->start;
669 default:
670 return 0;
671 }
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/*
675 * This is compatibility stuff for IO resources.
676 *
677 * Note how this, unlike the above, knows about
678 * the IO flag meanings (busy etc).
679 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700680 * request_region creates a new busy region.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700682 * check_region returns non-zero if the area is already busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700684 * release_region releases a matching busy region.
685 */
686
Alan Cox8b6d0432010-03-29 19:38:00 +0200687static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
688
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700689/**
690 * __request_region - create a new busy resource region
691 * @parent: parent resource descriptor
692 * @start: resource start address
693 * @n: resource region size
694 * @name: reserving caller's ID string
Randy Dunlap6ae301e2009-01-15 13:51:01 -0800695 * @flags: IO resource flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700697struct resource * __request_region(struct resource *parent,
698 resource_size_t start, resource_size_t n,
Arjan van de Vene8de1482008-10-22 19:55:31 -0700699 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Alan Cox8b6d0432010-03-29 19:38:00 +0200701 DECLARE_WAITQUEUE(wait, current);
Pekka J Enbergdd392712005-09-06 15:18:31 -0700702 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700704 if (!res)
705 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700707 res->name = name;
708 res->start = start;
709 res->end = start + n - 1;
710 res->flags = IORESOURCE_BUSY;
Arjan van de Vene8de1482008-10-22 19:55:31 -0700711 res->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700713 write_lock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700715 for (;;) {
716 struct resource *conflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700718 conflict = __request_resource(parent, res);
719 if (!conflict)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700721 if (conflict != parent) {
722 parent = conflict;
723 if (!(conflict->flags & IORESOURCE_BUSY))
724 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Alan Cox8b6d0432010-03-29 19:38:00 +0200726 if (conflict->flags & flags & IORESOURCE_MUXED) {
727 add_wait_queue(&muxed_resource_wait, &wait);
728 write_unlock(&resource_lock);
729 set_current_state(TASK_UNINTERRUPTIBLE);
730 schedule();
731 remove_wait_queue(&muxed_resource_wait, &wait);
732 write_lock(&resource_lock);
733 continue;
734 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700735 /* Uhhuh, that didn't work out.. */
736 kfree(res);
737 res = NULL;
738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700740 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return res;
742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743EXPORT_SYMBOL(__request_region);
744
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700745/**
746 * __check_region - check if a resource region is busy or free
747 * @parent: parent resource descriptor
748 * @start: resource start address
749 * @n: resource region size
750 *
751 * Returns 0 if the region is free at the moment it is checked,
752 * returns %-EBUSY if the region is busy.
753 *
754 * NOTE:
755 * This function is deprecated because its use is racy.
756 * Even if it returns 0, a subsequent call to request_region()
757 * may fail because another driver etc. just allocated the region.
758 * Do NOT use it. It will be removed from the kernel.
759 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700760int __check_region(struct resource *parent, resource_size_t start,
761 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct resource * res;
764
Arjan van de Vene8de1482008-10-22 19:55:31 -0700765 res = __request_region(parent, start, n, "check-region", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!res)
767 return -EBUSY;
768
769 release_resource(res);
770 kfree(res);
771 return 0;
772}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773EXPORT_SYMBOL(__check_region);
774
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700775/**
776 * __release_region - release a previously reserved resource region
777 * @parent: parent resource descriptor
778 * @start: resource start address
779 * @n: resource region size
780 *
781 * The described resource region must match a currently busy region.
782 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700783void __release_region(struct resource *parent, resource_size_t start,
784 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct resource **p;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700787 resource_size_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 p = &parent->child;
790 end = start + n - 1;
791
792 write_lock(&resource_lock);
793
794 for (;;) {
795 struct resource *res = *p;
796
797 if (!res)
798 break;
799 if (res->start <= start && res->end >= end) {
800 if (!(res->flags & IORESOURCE_BUSY)) {
801 p = &res->child;
802 continue;
803 }
804 if (res->start != start || res->end != end)
805 break;
806 *p = res->sibling;
807 write_unlock(&resource_lock);
Alan Cox8b6d0432010-03-29 19:38:00 +0200808 if (res->flags & IORESOURCE_MUXED)
809 wake_up(&muxed_resource_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 kfree(res);
811 return;
812 }
813 p = &res->sibling;
814 }
815
816 write_unlock(&resource_lock);
817
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700818 printk(KERN_WARNING "Trying to free nonexistent resource "
819 "<%016llx-%016llx>\n", (unsigned long long)start,
820 (unsigned long long)end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822EXPORT_SYMBOL(__release_region);
823
824/*
Tejun Heo9ac78492007-01-20 16:00:26 +0900825 * Managed region resource
826 */
827struct region_devres {
828 struct resource *parent;
829 resource_size_t start;
830 resource_size_t n;
831};
832
833static void devm_region_release(struct device *dev, void *res)
834{
835 struct region_devres *this = res;
836
837 __release_region(this->parent, this->start, this->n);
838}
839
840static int devm_region_match(struct device *dev, void *res, void *match_data)
841{
842 struct region_devres *this = res, *match = match_data;
843
844 return this->parent == match->parent &&
845 this->start == match->start && this->n == match->n;
846}
847
848struct resource * __devm_request_region(struct device *dev,
849 struct resource *parent, resource_size_t start,
850 resource_size_t n, const char *name)
851{
852 struct region_devres *dr = NULL;
853 struct resource *res;
854
855 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
856 GFP_KERNEL);
857 if (!dr)
858 return NULL;
859
860 dr->parent = parent;
861 dr->start = start;
862 dr->n = n;
863
Arjan van de Vene8de1482008-10-22 19:55:31 -0700864 res = __request_region(parent, start, n, name, 0);
Tejun Heo9ac78492007-01-20 16:00:26 +0900865 if (res)
866 devres_add(dev, dr);
867 else
868 devres_free(dr);
869
870 return res;
871}
872EXPORT_SYMBOL(__devm_request_region);
873
874void __devm_release_region(struct device *dev, struct resource *parent,
875 resource_size_t start, resource_size_t n)
876{
877 struct region_devres match_data = { parent, start, n };
878
879 __release_region(parent, start, n);
880 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
881 &match_data));
882}
883EXPORT_SYMBOL(__devm_release_region);
884
885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Called from init/main.c to reserve IO ports.
887 */
888#define MAXRESERVE 4
889static int __init reserve_setup(char *str)
890{
891 static int reserved;
892 static struct resource reserve[MAXRESERVE];
893
894 for (;;) {
Zhang Rui8bc1ad72009-06-30 11:41:31 -0700895 unsigned int io_start, io_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int x = reserved;
897
898 if (get_option (&str, &io_start) != 2)
899 break;
900 if (get_option (&str, &io_num) == 0)
901 break;
902 if (x < MAXRESERVE) {
903 struct resource *res = reserve + x;
904 res->name = "reserved";
905 res->start = io_start;
906 res->end = io_start + io_num - 1;
907 res->flags = IORESOURCE_BUSY;
908 res->child = NULL;
909 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
910 reserved = x+1;
911 }
912 }
913 return 1;
914}
915
916__setup("reserve=", reserve_setup);
Suresh Siddha379daf62008-09-25 18:43:34 -0700917
918/*
919 * Check if the requested addr and size spans more than any slot in the
920 * iomem resource tree.
921 */
922int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
923{
924 struct resource *p = &iomem_resource;
925 int err = 0;
926 loff_t l;
927
928 read_lock(&resource_lock);
929 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
930 /*
931 * We can probably skip the resources without
932 * IORESOURCE_IO attribute?
933 */
934 if (p->start >= addr + size)
935 continue;
936 if (p->end < addr)
937 continue;
Suresh Siddhad68612b2008-10-28 11:45:42 -0700938 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
939 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
Suresh Siddha379daf62008-09-25 18:43:34 -0700940 continue;
Arjan van de Ven3ac52662008-12-13 09:15:27 -0800941 /*
942 * if a resource is "BUSY", it's not a hardware resource
943 * but a driver mapping of such a resource; we don't want
944 * to warn for those; some drivers legitimately map only
945 * partial hardware resources. (example: vesafb)
946 */
947 if (p->flags & IORESOURCE_BUSY)
948 continue;
949
Suresh Siddha379daf62008-09-25 18:43:34 -0700950 printk(KERN_WARNING "resource map sanity check conflict: "
951 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
Ingo Molnar13eb8372008-09-26 10:10:12 +0200952 (unsigned long long)addr,
953 (unsigned long long)(addr + size - 1),
954 (unsigned long long)p->start,
955 (unsigned long long)p->end,
956 p->name);
Suresh Siddha379daf62008-09-25 18:43:34 -0700957 err = -1;
958 break;
959 }
960 read_unlock(&resource_lock);
961
962 return err;
963}
Arjan van de Vene8de1482008-10-22 19:55:31 -0700964
965#ifdef CONFIG_STRICT_DEVMEM
966static int strict_iomem_checks = 1;
967#else
968static int strict_iomem_checks;
969#endif
970
971/*
972 * check if an address is reserved in the iomem resource tree
973 * returns 1 if reserved, 0 if not reserved.
974 */
975int iomem_is_exclusive(u64 addr)
976{
977 struct resource *p = &iomem_resource;
978 int err = 0;
979 loff_t l;
980 int size = PAGE_SIZE;
981
982 if (!strict_iomem_checks)
983 return 0;
984
985 addr = addr & PAGE_MASK;
986
987 read_lock(&resource_lock);
988 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
989 /*
990 * We can probably skip the resources without
991 * IORESOURCE_IO attribute?
992 */
993 if (p->start >= addr + size)
994 break;
995 if (p->end < addr)
996 continue;
997 if (p->flags & IORESOURCE_BUSY &&
998 p->flags & IORESOURCE_EXCLUSIVE) {
999 err = 1;
1000 break;
1001 }
1002 }
1003 read_unlock(&resource_lock);
1004
1005 return err;
1006}
1007
1008static int __init strict_iomem(char *str)
1009{
1010 if (strstr(str, "relaxed"))
1011 strict_iomem_checks = 0;
1012 if (strstr(str, "strict"))
1013 strict_iomem_checks = 1;
1014 return 1;
1015}
1016
1017__setup("iomem=", strict_iomem);