blob: 7dc8ad24f915bedf9aa485b640468e99b630b705 [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-Hartman685143ac2006-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-Hartman685143ac2006-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 Hiroyuki75884fb12007-10-16 01:26:10 -0700321{
322 struct resource res;
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800323 unsigned long pfn, end_pfn;
KAMEZAWA Hiroyuki75884fb12007-10-16 01:26:10 -0700324 u64 orig_end;
325 int ret = -1;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700326
KAMEZAWA Hiroyuki75884fb12007-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 Hiroyuki75884fb12007-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 Hiroyuki75884fb12007-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
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600360static resource_size_t simple_align_resource(void *data,
361 const struct resource *avail,
362 resource_size_t size,
363 resource_size_t align)
364{
365 return avail->start;
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * Find empty slot in the resource tree given range and alignment.
370 */
371static int find_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700372 resource_size_t size, resource_size_t min,
373 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100374 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100375 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100376 resource_size_t,
377 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 void *alignf_data)
379{
380 struct resource *this = root->child;
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100381 struct resource tmp = *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100383 tmp.start = root->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /*
385 * Skip past an allocated resource that starts at 0, since the assignment
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100386 * of this->start - 1 to tmp->end below would cause an underflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388 if (this && this->start == 0) {
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100389 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 this = this->sibling;
391 }
392 for(;;) {
393 if (this)
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100394 tmp.end = this->start - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 else
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100396 tmp.end = root->end;
397 if (tmp.start < min)
398 tmp.start = min;
399 if (tmp.end > max)
400 tmp.end = max;
401 tmp.start = ALIGN(tmp.start, align);
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600402
403 tmp.start = alignf(alignf_data, &tmp, size, align);
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100404 if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
405 new->start = tmp.start;
406 new->end = tmp.start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
408 }
409 if (!this)
410 break;
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100411 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 this = this->sibling;
413 }
414 return -EBUSY;
415}
416
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700417/**
418 * allocate_resource - allocate empty slot in the resource tree given range & alignment
419 * @root: root resource descriptor
420 * @new: resource descriptor desired by caller
421 * @size: requested resource region size
422 * @min: minimum size to allocate
423 * @max: maximum size to allocate
424 * @align: alignment requested, in bytes
425 * @alignf: alignment function, optional, called if not NULL
426 * @alignf_data: arbitrary data to pass to the @alignf function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
428int allocate_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700429 resource_size_t size, resource_size_t min,
430 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100431 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100432 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100433 resource_size_t,
434 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 void *alignf_data)
436{
437 int err;
438
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600439 if (!alignf)
440 alignf = simple_align_resource;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 write_lock(&resource_lock);
443 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
444 if (err >= 0 && __request_resource(root, new))
445 err = -EBUSY;
446 write_unlock(&resource_lock);
447 return err;
448}
449
450EXPORT_SYMBOL(allocate_resource);
451
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700452/*
453 * Insert a resource into the resource tree. If successful, return NULL,
454 * otherwise return the conflicting resource (compare to __request_resource())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700456static struct resource * __insert_resource(struct resource *parent, struct resource *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct resource *first, *next;
459
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700460 for (;; parent = first) {
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700461 first = __request_resource(parent, new);
462 if (!first)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700463 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700465 if (first == parent)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700466 return first;
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700467
468 if ((first->start > new->start) || (first->end < new->end))
469 break;
470 if ((first->start == new->start) && (first->end == new->end))
471 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 for (next = first; ; next = next->sibling) {
475 /* Partial overlap? Bad, and unfixable */
476 if (next->start < new->start || next->end > new->end)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700477 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!next->sibling)
479 break;
480 if (next->sibling->start > new->end)
481 break;
482 }
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 new->parent = parent;
485 new->sibling = next->sibling;
486 new->child = first;
487
488 next->sibling = NULL;
489 for (next = first; next; next = next->sibling)
490 next->parent = new;
491
492 if (parent->child == first) {
493 parent->child = new;
494 } else {
495 next = parent->child;
496 while (next->sibling != first)
497 next = next->sibling;
498 next->sibling = new;
499 }
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700500 return NULL;
501}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700503/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700504 * insert_resource_conflict - Inserts resource in the resource tree
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700505 * @parent: parent of the new resource
506 * @new: new resource to insert
507 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700508 * Returns 0 on success, conflict resource if the resource can't be inserted.
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700509 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700510 * This function is equivalent to request_resource_conflict when no conflict
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700511 * happens. If a conflict happens, and the conflicting resources
512 * entirely fit within the range of the new resource, then the new
513 * resource is inserted and the conflicting resources become children of
514 * the new resource.
515 */
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700516struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700517{
518 struct resource *conflict;
519
520 write_lock(&resource_lock);
521 conflict = __insert_resource(parent, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 write_unlock(&resource_lock);
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700523 return conflict;
524}
525
526/**
527 * insert_resource - Inserts a resource in the resource tree
528 * @parent: parent of the new resource
529 * @new: new resource to insert
530 *
531 * Returns 0 on success, -EBUSY if the resource can't be inserted.
532 */
533int insert_resource(struct resource *parent, struct resource *new)
534{
535 struct resource *conflict;
536
537 conflict = insert_resource_conflict(parent, new);
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700538 return conflict ? -EBUSY : 0;
539}
540
541/**
542 * insert_resource_expand_to_fit - Insert a resource into the resource tree
Randy Dunlap6781f4a2008-08-31 20:31:55 -0700543 * @root: root resource descriptor
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700544 * @new: new resource to insert
545 *
546 * Insert a resource into the resource tree, possibly expanding it in order
547 * to make it encompass any conflicting resources.
548 */
549void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
550{
551 if (new->parent)
552 return;
553
554 write_lock(&resource_lock);
555 for (;;) {
556 struct resource *conflict;
557
558 conflict = __insert_resource(root, new);
559 if (!conflict)
560 break;
561 if (conflict == root)
562 break;
563
564 /* Ok, expand resource to cover the conflict, then try again .. */
565 if (conflict->start < new->start)
566 new->start = conflict->start;
567 if (conflict->end > new->end)
568 new->end = conflict->end;
569
570 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
571 }
572 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700575/**
576 * adjust_resource - modify a resource's start and size
577 * @res: resource to modify
578 * @start: new start value
579 * @size: new size
580 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * Given an existing resource, change its start and size to match the
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700582 * arguments. Returns 0 on success, -EBUSY if it can't fit.
583 * Existing children of the resource are assumed to be immutable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700585int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct resource *tmp, *parent = res->parent;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700588 resource_size_t end = start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int result = -EBUSY;
590
591 write_lock(&resource_lock);
592
593 if ((start < parent->start) || (end > parent->end))
594 goto out;
595
596 for (tmp = res->child; tmp; tmp = tmp->sibling) {
597 if ((tmp->start < start) || (tmp->end > end))
598 goto out;
599 }
600
601 if (res->sibling && (res->sibling->start <= end))
602 goto out;
603
604 tmp = parent->child;
605 if (tmp != res) {
606 while (tmp->sibling != res)
607 tmp = tmp->sibling;
608 if (start <= tmp->end)
609 goto out;
610 }
611
612 res->start = start;
613 res->end = end;
614 result = 0;
615
616 out:
617 write_unlock(&resource_lock);
618 return result;
619}
620
Yinghai Lu268364a2008-09-04 21:02:44 +0200621static void __init __reserve_region_with_split(struct resource *root,
622 resource_size_t start, resource_size_t end,
623 const char *name)
624{
625 struct resource *parent = root;
626 struct resource *conflict;
Linus Torvalds42c02022008-11-01 09:53:58 -0700627 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
Yinghai Lu268364a2008-09-04 21:02:44 +0200628
629 if (!res)
630 return;
631
632 res->name = name;
633 res->start = start;
634 res->end = end;
635 res->flags = IORESOURCE_BUSY;
636
Linus Torvaldsff542502009-04-18 21:44:24 -0700637 conflict = __request_resource(parent, res);
638 if (!conflict)
639 return;
Yinghai Lu268364a2008-09-04 21:02:44 +0200640
Linus Torvaldsff542502009-04-18 21:44:24 -0700641 /* failed, split and try again */
642 kfree(res);
Yinghai Lu268364a2008-09-04 21:02:44 +0200643
Linus Torvaldsff542502009-04-18 21:44:24 -0700644 /* conflict covered whole area */
645 if (conflict->start <= start && conflict->end >= end)
646 return;
Yinghai Lu268364a2008-09-04 21:02:44 +0200647
Linus Torvaldsff542502009-04-18 21:44:24 -0700648 if (conflict->start > start)
649 __reserve_region_with_split(root, start, conflict->start-1, name);
650 if (conflict->end < end)
651 __reserve_region_with_split(root, conflict->end+1, end, name);
Yinghai Lu268364a2008-09-04 21:02:44 +0200652}
653
Paul Mundtbea92112008-10-22 19:31:11 +0900654void __init reserve_region_with_split(struct resource *root,
Yinghai Lu268364a2008-09-04 21:02:44 +0200655 resource_size_t start, resource_size_t end,
656 const char *name)
657{
658 write_lock(&resource_lock);
659 __reserve_region_with_split(root, start, end, name);
660 write_unlock(&resource_lock);
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663EXPORT_SYMBOL(adjust_resource);
664
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400665/**
666 * resource_alignment - calculate resource's alignment
667 * @res: resource pointer
668 *
669 * Returns alignment on success, 0 (invalid alignment) on failure.
670 */
671resource_size_t resource_alignment(struct resource *res)
672{
673 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
674 case IORESOURCE_SIZEALIGN:
Magnus Damm1a4e5642008-07-29 22:32:57 -0700675 return resource_size(res);
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400676 case IORESOURCE_STARTALIGN:
677 return res->start;
678 default:
679 return 0;
680 }
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/*
684 * This is compatibility stuff for IO resources.
685 *
686 * Note how this, unlike the above, knows about
687 * the IO flag meanings (busy etc).
688 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700689 * request_region creates a new busy region.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700691 * check_region returns non-zero if the area is already busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 *
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700693 * release_region releases a matching busy region.
694 */
695
Alan Cox8b6d0432010-03-29 19:38:00 +0200696static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
697
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700698/**
699 * __request_region - create a new busy resource region
700 * @parent: parent resource descriptor
701 * @start: resource start address
702 * @n: resource region size
703 * @name: reserving caller's ID string
Randy Dunlap6ae301e2009-01-15 13:51:01 -0800704 * @flags: IO resource flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700706struct resource * __request_region(struct resource *parent,
707 resource_size_t start, resource_size_t n,
Arjan van de Vene8de1482008-10-22 19:55:31 -0700708 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Alan Cox8b6d0432010-03-29 19:38:00 +0200710 DECLARE_WAITQUEUE(wait, current);
Pekka J Enbergdd392712005-09-06 15:18:31 -0700711 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700713 if (!res)
714 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700716 res->name = name;
717 res->start = start;
718 res->end = start + n - 1;
719 res->flags = IORESOURCE_BUSY;
Arjan van de Vene8de1482008-10-22 19:55:31 -0700720 res->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700722 write_lock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700724 for (;;) {
725 struct resource *conflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700727 conflict = __request_resource(parent, res);
728 if (!conflict)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700730 if (conflict != parent) {
731 parent = conflict;
732 if (!(conflict->flags & IORESOURCE_BUSY))
733 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Alan Cox8b6d0432010-03-29 19:38:00 +0200735 if (conflict->flags & flags & IORESOURCE_MUXED) {
736 add_wait_queue(&muxed_resource_wait, &wait);
737 write_unlock(&resource_lock);
738 set_current_state(TASK_UNINTERRUPTIBLE);
739 schedule();
740 remove_wait_queue(&muxed_resource_wait, &wait);
741 write_lock(&resource_lock);
742 continue;
743 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700744 /* Uhhuh, that didn't work out.. */
745 kfree(res);
746 res = NULL;
747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -0700749 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return res;
751}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752EXPORT_SYMBOL(__request_region);
753
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700754/**
755 * __check_region - check if a resource region is busy or free
756 * @parent: parent resource descriptor
757 * @start: resource start address
758 * @n: resource region size
759 *
760 * Returns 0 if the region is free at the moment it is checked,
761 * returns %-EBUSY if the region is busy.
762 *
763 * NOTE:
764 * This function is deprecated because its use is racy.
765 * Even if it returns 0, a subsequent call to request_region()
766 * may fail because another driver etc. just allocated the region.
767 * Do NOT use it. It will be removed from the kernel.
768 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700769int __check_region(struct resource *parent, resource_size_t start,
770 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct resource * res;
773
Arjan van de Vene8de1482008-10-22 19:55:31 -0700774 res = __request_region(parent, start, n, "check-region", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!res)
776 return -EBUSY;
777
778 release_resource(res);
779 kfree(res);
780 return 0;
781}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782EXPORT_SYMBOL(__check_region);
783
Randy Dunlape1ca66d2006-10-03 01:13:51 -0700784/**
785 * __release_region - release a previously reserved resource region
786 * @parent: parent resource descriptor
787 * @start: resource start address
788 * @n: resource region size
789 *
790 * The described resource region must match a currently busy region.
791 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700792void __release_region(struct resource *parent, resource_size_t start,
793 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 struct resource **p;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700796 resource_size_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 p = &parent->child;
799 end = start + n - 1;
800
801 write_lock(&resource_lock);
802
803 for (;;) {
804 struct resource *res = *p;
805
806 if (!res)
807 break;
808 if (res->start <= start && res->end >= end) {
809 if (!(res->flags & IORESOURCE_BUSY)) {
810 p = &res->child;
811 continue;
812 }
813 if (res->start != start || res->end != end)
814 break;
815 *p = res->sibling;
816 write_unlock(&resource_lock);
Alan Cox8b6d0432010-03-29 19:38:00 +0200817 if (res->flags & IORESOURCE_MUXED)
818 wake_up(&muxed_resource_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 kfree(res);
820 return;
821 }
822 p = &res->sibling;
823 }
824
825 write_unlock(&resource_lock);
826
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -0700827 printk(KERN_WARNING "Trying to free nonexistent resource "
828 "<%016llx-%016llx>\n", (unsigned long long)start,
829 (unsigned long long)end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831EXPORT_SYMBOL(__release_region);
832
833/*
Tejun Heo9ac78492007-01-20 16:00:26 +0900834 * Managed region resource
835 */
836struct region_devres {
837 struct resource *parent;
838 resource_size_t start;
839 resource_size_t n;
840};
841
842static void devm_region_release(struct device *dev, void *res)
843{
844 struct region_devres *this = res;
845
846 __release_region(this->parent, this->start, this->n);
847}
848
849static int devm_region_match(struct device *dev, void *res, void *match_data)
850{
851 struct region_devres *this = res, *match = match_data;
852
853 return this->parent == match->parent &&
854 this->start == match->start && this->n == match->n;
855}
856
857struct resource * __devm_request_region(struct device *dev,
858 struct resource *parent, resource_size_t start,
859 resource_size_t n, const char *name)
860{
861 struct region_devres *dr = NULL;
862 struct resource *res;
863
864 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
865 GFP_KERNEL);
866 if (!dr)
867 return NULL;
868
869 dr->parent = parent;
870 dr->start = start;
871 dr->n = n;
872
Arjan van de Vene8de1482008-10-22 19:55:31 -0700873 res = __request_region(parent, start, n, name, 0);
Tejun Heo9ac78492007-01-20 16:00:26 +0900874 if (res)
875 devres_add(dev, dr);
876 else
877 devres_free(dr);
878
879 return res;
880}
881EXPORT_SYMBOL(__devm_request_region);
882
883void __devm_release_region(struct device *dev, struct resource *parent,
884 resource_size_t start, resource_size_t n)
885{
886 struct region_devres match_data = { parent, start, n };
887
888 __release_region(parent, start, n);
889 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
890 &match_data));
891}
892EXPORT_SYMBOL(__devm_release_region);
893
894/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * Called from init/main.c to reserve IO ports.
896 */
897#define MAXRESERVE 4
898static int __init reserve_setup(char *str)
899{
900 static int reserved;
901 static struct resource reserve[MAXRESERVE];
902
903 for (;;) {
Zhang Rui8bc1ad72009-06-30 11:41:31 -0700904 unsigned int io_start, io_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 int x = reserved;
906
907 if (get_option (&str, &io_start) != 2)
908 break;
909 if (get_option (&str, &io_num) == 0)
910 break;
911 if (x < MAXRESERVE) {
912 struct resource *res = reserve + x;
913 res->name = "reserved";
914 res->start = io_start;
915 res->end = io_start + io_num - 1;
916 res->flags = IORESOURCE_BUSY;
917 res->child = NULL;
918 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
919 reserved = x+1;
920 }
921 }
922 return 1;
923}
924
925__setup("reserve=", reserve_setup);
Suresh Siddha379daf622008-09-25 18:43:34 -0700926
927/*
928 * Check if the requested addr and size spans more than any slot in the
929 * iomem resource tree.
930 */
931int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
932{
933 struct resource *p = &iomem_resource;
934 int err = 0;
935 loff_t l;
936
937 read_lock(&resource_lock);
938 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
939 /*
940 * We can probably skip the resources without
941 * IORESOURCE_IO attribute?
942 */
943 if (p->start >= addr + size)
944 continue;
945 if (p->end < addr)
946 continue;
Suresh Siddhad68612b2008-10-28 11:45:42 -0700947 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
948 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
Suresh Siddha379daf622008-09-25 18:43:34 -0700949 continue;
Arjan van de Ven3ac52662008-12-13 09:15:27 -0800950 /*
951 * if a resource is "BUSY", it's not a hardware resource
952 * but a driver mapping of such a resource; we don't want
953 * to warn for those; some drivers legitimately map only
954 * partial hardware resources. (example: vesafb)
955 */
956 if (p->flags & IORESOURCE_BUSY)
957 continue;
958
Suresh Siddha379daf622008-09-25 18:43:34 -0700959 printk(KERN_WARNING "resource map sanity check conflict: "
960 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
Ingo Molnar13eb8372008-09-26 10:10:12 +0200961 (unsigned long long)addr,
962 (unsigned long long)(addr + size - 1),
963 (unsigned long long)p->start,
964 (unsigned long long)p->end,
965 p->name);
Suresh Siddha379daf622008-09-25 18:43:34 -0700966 err = -1;
967 break;
968 }
969 read_unlock(&resource_lock);
970
971 return err;
972}
Arjan van de Vene8de1482008-10-22 19:55:31 -0700973
974#ifdef CONFIG_STRICT_DEVMEM
975static int strict_iomem_checks = 1;
976#else
977static int strict_iomem_checks;
978#endif
979
980/*
981 * check if an address is reserved in the iomem resource tree
982 * returns 1 if reserved, 0 if not reserved.
983 */
984int iomem_is_exclusive(u64 addr)
985{
986 struct resource *p = &iomem_resource;
987 int err = 0;
988 loff_t l;
989 int size = PAGE_SIZE;
990
991 if (!strict_iomem_checks)
992 return 0;
993
994 addr = addr & PAGE_MASK;
995
996 read_lock(&resource_lock);
997 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
998 /*
999 * We can probably skip the resources without
1000 * IORESOURCE_IO attribute?
1001 */
1002 if (p->start >= addr + size)
1003 break;
1004 if (p->end < addr)
1005 continue;
1006 if (p->flags & IORESOURCE_BUSY &&
1007 p->flags & IORESOURCE_EXCLUSIVE) {
1008 err = 1;
1009 break;
1010 }
1011 }
1012 read_unlock(&resource_lock);
1013
1014 return err;
1015}
1016
1017static int __init strict_iomem(char *str)
1018{
1019 if (strstr(str, "relaxed"))
1020 strict_iomem_checks = 0;
1021 if (strstr(str, "strict"))
1022 strict_iomem_checks = 1;
1023 return 1;
1024}
1025
1026__setup("iomem=", strict_iomem);