x86, paravirt: Remove kmap_atomic_pte paravirt op.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / resource.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4 10#include <linux/module.h>
1da177e4
LT
11#include <linux/errno.h>
12#include <linux/ioport.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/fs.h>
17#include <linux/proc_fs.h>
18#include <linux/seq_file.h>
9ac7849e 19#include <linux/device.h>
d68612b2 20#include <linux/pfn.h>
1da177e4
LT
21#include <asm/io.h>
22
23
24struct resource ioport_resource = {
25 .name = "PCI IO",
6550e07f 26 .start = 0,
1da177e4
LT
27 .end = IO_SPACE_LIMIT,
28 .flags = IORESOURCE_IO,
29};
1da177e4
LT
30EXPORT_SYMBOL(ioport_resource);
31
32struct resource iomem_resource = {
33 .name = "PCI mem",
6550e07f
GKH
34 .start = 0,
35 .end = -1,
1da177e4
LT
36 .flags = IORESOURCE_MEM,
37};
1da177e4
LT
38EXPORT_SYMBOL(iomem_resource);
39
40static DEFINE_RWLOCK(resource_lock);
41
1da177e4
LT
42static void *r_next(struct seq_file *m, void *v, loff_t *pos)
43{
44 struct resource *p = v;
45 (*pos)++;
46 if (p->child)
47 return p->child;
48 while (!p->sibling && p->parent)
49 p = p->parent;
50 return p->sibling;
51}
52
13eb8375
IM
53#ifdef CONFIG_PROC_FS
54
55enum { MAX_IORES_LEVEL = 5 };
56
1da177e4
LT
57static void *r_start(struct seq_file *m, loff_t *pos)
58 __acquires(resource_lock)
59{
60 struct resource *p = m->private;
61 loff_t l = 0;
62 read_lock(&resource_lock);
63 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
64 ;
65 return p;
66}
67
68static void r_stop(struct seq_file *m, void *v)
69 __releases(resource_lock)
70{
71 read_unlock(&resource_lock);
72}
73
74static int r_show(struct seq_file *m, void *v)
75{
76 struct resource *root = m->private;
77 struct resource *r = v, *p;
78 int width = root->end < 0x10000 ? 4 : 8;
79 int depth;
80
81 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
82 if (p->parent == root)
83 break;
685143ac 84 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
1da177e4 85 depth * 2, "",
685143ac
GKH
86 width, (unsigned long long) r->start,
87 width, (unsigned long long) r->end,
1da177e4
LT
88 r->name ? r->name : "<BAD>");
89 return 0;
90}
91
15ad7cdc 92static const struct seq_operations resource_op = {
1da177e4
LT
93 .start = r_start,
94 .next = r_next,
95 .stop = r_stop,
96 .show = r_show,
97};
98
99static int ioports_open(struct inode *inode, struct file *file)
100{
101 int res = seq_open(file, &resource_op);
102 if (!res) {
103 struct seq_file *m = file->private_data;
104 m->private = &ioport_resource;
105 }
106 return res;
107}
108
109static int iomem_open(struct inode *inode, struct file *file)
110{
111 int res = seq_open(file, &resource_op);
112 if (!res) {
113 struct seq_file *m = file->private_data;
114 m->private = &iomem_resource;
115 }
116 return res;
117}
118
15ad7cdc 119static const struct file_operations proc_ioports_operations = {
1da177e4
LT
120 .open = ioports_open,
121 .read = seq_read,
122 .llseek = seq_lseek,
123 .release = seq_release,
124};
125
15ad7cdc 126static const struct file_operations proc_iomem_operations = {
1da177e4
LT
127 .open = iomem_open,
128 .read = seq_read,
129 .llseek = seq_lseek,
130 .release = seq_release,
131};
132
133static int __init ioresources_init(void)
134{
c33fff0a
DL
135 proc_create("ioports", 0, NULL, &proc_ioports_operations);
136 proc_create("iomem", 0, NULL, &proc_iomem_operations);
1da177e4
LT
137 return 0;
138}
139__initcall(ioresources_init);
140
141#endif /* CONFIG_PROC_FS */
142
143/* Return the conflict entry if you can't request it */
144static struct resource * __request_resource(struct resource *root, struct resource *new)
145{
d75fc8bb
GKH
146 resource_size_t start = new->start;
147 resource_size_t end = new->end;
1da177e4
LT
148 struct resource *tmp, **p;
149
150 if (end < start)
151 return root;
152 if (start < root->start)
153 return root;
154 if (end > root->end)
155 return root;
156 p = &root->child;
157 for (;;) {
158 tmp = *p;
159 if (!tmp || tmp->start > end) {
160 new->sibling = tmp;
161 *p = new;
162 new->parent = root;
163 return NULL;
164 }
165 p = &tmp->sibling;
166 if (tmp->end < start)
167 continue;
168 return tmp;
169 }
170}
171
172static int __release_resource(struct resource *old)
173{
174 struct resource *tmp, **p;
175
176 p = &old->parent->child;
177 for (;;) {
178 tmp = *p;
179 if (!tmp)
180 break;
181 if (tmp == old) {
182 *p = tmp->sibling;
183 old->parent = NULL;
184 return 0;
185 }
186 p = &tmp->sibling;
187 }
188 return -EINVAL;
189}
190
e1ca66d1
RD
191/**
192 * request_resource - request and reserve an I/O or memory resource
193 * @root: root resource descriptor
194 * @new: resource descriptor desired by caller
195 *
196 * Returns 0 for success, negative error code on error.
197 */
1da177e4
LT
198int request_resource(struct resource *root, struct resource *new)
199{
200 struct resource *conflict;
201
202 write_lock(&resource_lock);
203 conflict = __request_resource(root, new);
204 write_unlock(&resource_lock);
205 return conflict ? -EBUSY : 0;
206}
207
208EXPORT_SYMBOL(request_resource);
209
e1ca66d1
RD
210/**
211 * release_resource - release a previously reserved resource
212 * @old: resource pointer
213 */
1da177e4
LT
214int release_resource(struct resource *old)
215{
216 int retval;
217
218 write_lock(&resource_lock);
219 retval = __release_resource(old);
220 write_unlock(&resource_lock);
221 return retval;
222}
223
224EXPORT_SYMBOL(release_resource);
225
908eedc6 226#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
2842f114
KH
227/*
228 * Finds the lowest memory reosurce exists within [res->start.res->end)
908eedc6 229 * the caller must specify res->start, res->end, res->flags and "name".
2842f114
KH
230 * If found, returns 0, res is overwritten, if not found, returns -1.
231 */
908eedc6 232static int find_next_system_ram(struct resource *res, char *name)
2842f114
KH
233{
234 resource_size_t start, end;
235 struct resource *p;
236
237 BUG_ON(!res);
238
239 start = res->start;
240 end = res->end;
58c1b5b0 241 BUG_ON(start >= end);
2842f114
KH
242
243 read_lock(&resource_lock);
244 for (p = iomem_resource.child; p ; p = p->sibling) {
245 /* system ram is just marked as IORESOURCE_MEM */
246 if (p->flags != res->flags)
247 continue;
908eedc6
KH
248 if (name && strcmp(p->name, name))
249 continue;
2842f114
KH
250 if (p->start > end) {
251 p = NULL;
252 break;
253 }
58c1b5b0 254 if ((p->end >= start) && (p->start < end))
2842f114
KH
255 break;
256 }
257 read_unlock(&resource_lock);
258 if (!p)
259 return -1;
260 /* copy data */
0f04ab5e
KH
261 if (res->start < p->start)
262 res->start = p->start;
263 if (res->end > p->end)
264 res->end = p->end;
2842f114
KH
265 return 0;
266}
908eedc6
KH
267
268/*
269 * This function calls callback against all memory range of "System RAM"
270 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
271 * Now, this function is only for "System RAM".
272 */
273int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
274 void *arg, int (*func)(unsigned long, unsigned long, void *))
75884fb1
KH
275{
276 struct resource res;
277 unsigned long pfn, len;
278 u64 orig_end;
279 int ret = -1;
908eedc6 280
75884fb1
KH
281 res.start = (u64) start_pfn << PAGE_SHIFT;
282 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
887c3cb1 283 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
75884fb1 284 orig_end = res.end;
908eedc6
KH
285 while ((res.start < res.end) &&
286 (find_next_system_ram(&res, "System RAM") >= 0)) {
75884fb1
KH
287 pfn = (unsigned long)(res.start >> PAGE_SHIFT);
288 len = (unsigned long)((res.end + 1 - res.start) >> PAGE_SHIFT);
289 ret = (*func)(pfn, len, arg);
290 if (ret)
291 break;
292 res.start = res.end + 1;
293 res.end = orig_end;
294 }
295 return ret;
296}
297
2842f114
KH
298#endif
299
61ef2489
WF
300static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
301{
302 return 1;
303}
304/*
305 * This generic page_is_ram() returns true if specified address is
306 * registered as "System RAM" in iomem_resource list.
307 */
e5273007 308int __weak page_is_ram(unsigned long pfn)
61ef2489
WF
309{
310 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
311}
312
1da177e4
LT
313/*
314 * Find empty slot in the resource tree given range and alignment.
315 */
316static int find_resource(struct resource *root, struct resource *new,
d75fc8bb
GKH
317 resource_size_t size, resource_size_t min,
318 resource_size_t max, resource_size_t align,
1da177e4 319 void (*alignf)(void *, struct resource *,
d75fc8bb 320 resource_size_t, resource_size_t),
1da177e4
LT
321 void *alignf_data)
322{
323 struct resource *this = root->child;
0e2c8b8f 324 struct resource tmp = *new;
1da177e4 325
0e2c8b8f 326 tmp.start = root->start;
1da177e4
LT
327 /*
328 * Skip past an allocated resource that starts at 0, since the assignment
0e2c8b8f 329 * of this->start - 1 to tmp->end below would cause an underflow.
1da177e4
LT
330 */
331 if (this && this->start == 0) {
0e2c8b8f 332 tmp.start = this->end + 1;
1da177e4
LT
333 this = this->sibling;
334 }
335 for(;;) {
336 if (this)
0e2c8b8f 337 tmp.end = this->start - 1;
1da177e4 338 else
0e2c8b8f
DB
339 tmp.end = root->end;
340 if (tmp.start < min)
341 tmp.start = min;
342 if (tmp.end > max)
343 tmp.end = max;
344 tmp.start = ALIGN(tmp.start, align);
1da177e4 345 if (alignf)
0e2c8b8f
DB
346 alignf(alignf_data, &tmp, size, align);
347 if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
348 new->start = tmp.start;
349 new->end = tmp.start + size - 1;
1da177e4
LT
350 return 0;
351 }
352 if (!this)
353 break;
0e2c8b8f 354 tmp.start = this->end + 1;
1da177e4
LT
355 this = this->sibling;
356 }
357 return -EBUSY;
358}
359
e1ca66d1
RD
360/**
361 * allocate_resource - allocate empty slot in the resource tree given range & alignment
362 * @root: root resource descriptor
363 * @new: resource descriptor desired by caller
364 * @size: requested resource region size
365 * @min: minimum size to allocate
366 * @max: maximum size to allocate
367 * @align: alignment requested, in bytes
368 * @alignf: alignment function, optional, called if not NULL
369 * @alignf_data: arbitrary data to pass to the @alignf function
1da177e4
LT
370 */
371int allocate_resource(struct resource *root, struct resource *new,
d75fc8bb
GKH
372 resource_size_t size, resource_size_t min,
373 resource_size_t max, resource_size_t align,
1da177e4 374 void (*alignf)(void *, struct resource *,
d75fc8bb 375 resource_size_t, resource_size_t),
1da177e4
LT
376 void *alignf_data)
377{
378 int err;
379
380 write_lock(&resource_lock);
381 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
382 if (err >= 0 && __request_resource(root, new))
383 err = -EBUSY;
384 write_unlock(&resource_lock);
385 return err;
386}
387
388EXPORT_SYMBOL(allocate_resource);
389
bef69ea0
LT
390/*
391 * Insert a resource into the resource tree. If successful, return NULL,
392 * otherwise return the conflicting resource (compare to __request_resource())
1da177e4 393 */
bef69ea0 394static struct resource * __insert_resource(struct resource *parent, struct resource *new)
1da177e4 395{
1da177e4
LT
396 struct resource *first, *next;
397
d33b6fba 398 for (;; parent = first) {
d33b6fba
MW
399 first = __request_resource(parent, new);
400 if (!first)
bef69ea0 401 return first;
d33b6fba 402
d33b6fba 403 if (first == parent)
bef69ea0 404 return first;
d33b6fba
MW
405
406 if ((first->start > new->start) || (first->end < new->end))
407 break;
408 if ((first->start == new->start) && (first->end == new->end))
409 break;
1da177e4
LT
410 }
411
412 for (next = first; ; next = next->sibling) {
413 /* Partial overlap? Bad, and unfixable */
414 if (next->start < new->start || next->end > new->end)
bef69ea0 415 return next;
1da177e4
LT
416 if (!next->sibling)
417 break;
418 if (next->sibling->start > new->end)
419 break;
420 }
421
1da177e4
LT
422 new->parent = parent;
423 new->sibling = next->sibling;
424 new->child = first;
425
426 next->sibling = NULL;
427 for (next = first; next; next = next->sibling)
428 next->parent = new;
429
430 if (parent->child == first) {
431 parent->child = new;
432 } else {
433 next = parent->child;
434 while (next->sibling != first)
435 next = next->sibling;
436 next->sibling = new;
437 }
bef69ea0
LT
438 return NULL;
439}
1da177e4 440
bef69ea0
LT
441/**
442 * insert_resource - Inserts a resource in the resource tree
443 * @parent: parent of the new resource
444 * @new: new resource to insert
445 *
446 * Returns 0 on success, -EBUSY if the resource can't be inserted.
447 *
448 * This function is equivalent to request_resource when no conflict
449 * happens. If a conflict happens, and the conflicting resources
450 * entirely fit within the range of the new resource, then the new
451 * resource is inserted and the conflicting resources become children of
452 * the new resource.
453 */
454int insert_resource(struct resource *parent, struct resource *new)
455{
456 struct resource *conflict;
457
458 write_lock(&resource_lock);
459 conflict = __insert_resource(parent, new);
460 write_unlock(&resource_lock);
461 return conflict ? -EBUSY : 0;
462}
463
464/**
465 * insert_resource_expand_to_fit - Insert a resource into the resource tree
6781f4ae 466 * @root: root resource descriptor
bef69ea0
LT
467 * @new: new resource to insert
468 *
469 * Insert a resource into the resource tree, possibly expanding it in order
470 * to make it encompass any conflicting resources.
471 */
472void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
473{
474 if (new->parent)
475 return;
476
477 write_lock(&resource_lock);
478 for (;;) {
479 struct resource *conflict;
480
481 conflict = __insert_resource(root, new);
482 if (!conflict)
483 break;
484 if (conflict == root)
485 break;
486
487 /* Ok, expand resource to cover the conflict, then try again .. */
488 if (conflict->start < new->start)
489 new->start = conflict->start;
490 if (conflict->end > new->end)
491 new->end = conflict->end;
492
493 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
494 }
1da177e4 495 write_unlock(&resource_lock);
1da177e4
LT
496}
497
e1ca66d1
RD
498/**
499 * adjust_resource - modify a resource's start and size
500 * @res: resource to modify
501 * @start: new start value
502 * @size: new size
503 *
1da177e4 504 * Given an existing resource, change its start and size to match the
e1ca66d1
RD
505 * arguments. Returns 0 on success, -EBUSY if it can't fit.
506 * Existing children of the resource are assumed to be immutable.
1da177e4 507 */
d75fc8bb 508int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
1da177e4
LT
509{
510 struct resource *tmp, *parent = res->parent;
d75fc8bb 511 resource_size_t end = start + size - 1;
1da177e4
LT
512 int result = -EBUSY;
513
514 write_lock(&resource_lock);
515
516 if ((start < parent->start) || (end > parent->end))
517 goto out;
518
519 for (tmp = res->child; tmp; tmp = tmp->sibling) {
520 if ((tmp->start < start) || (tmp->end > end))
521 goto out;
522 }
523
524 if (res->sibling && (res->sibling->start <= end))
525 goto out;
526
527 tmp = parent->child;
528 if (tmp != res) {
529 while (tmp->sibling != res)
530 tmp = tmp->sibling;
531 if (start <= tmp->end)
532 goto out;
533 }
534
535 res->start = start;
536 res->end = end;
537 result = 0;
538
539 out:
540 write_unlock(&resource_lock);
541 return result;
542}
543
268364a0
YL
544static void __init __reserve_region_with_split(struct resource *root,
545 resource_size_t start, resource_size_t end,
546 const char *name)
547{
548 struct resource *parent = root;
549 struct resource *conflict;
42c02023 550 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
268364a0
YL
551
552 if (!res)
553 return;
554
555 res->name = name;
556 res->start = start;
557 res->end = end;
558 res->flags = IORESOURCE_BUSY;
559
ff54250a
LT
560 conflict = __request_resource(parent, res);
561 if (!conflict)
562 return;
268364a0 563
ff54250a
LT
564 /* failed, split and try again */
565 kfree(res);
268364a0 566
ff54250a
LT
567 /* conflict covered whole area */
568 if (conflict->start <= start && conflict->end >= end)
569 return;
268364a0 570
ff54250a
LT
571 if (conflict->start > start)
572 __reserve_region_with_split(root, start, conflict->start-1, name);
573 if (conflict->end < end)
574 __reserve_region_with_split(root, conflict->end+1, end, name);
268364a0
YL
575}
576
bea92112 577void __init reserve_region_with_split(struct resource *root,
268364a0
YL
578 resource_size_t start, resource_size_t end,
579 const char *name)
580{
581 write_lock(&resource_lock);
582 __reserve_region_with_split(root, start, end, name);
583 write_unlock(&resource_lock);
584}
585
1da177e4
LT
586EXPORT_SYMBOL(adjust_resource);
587
88452565
IK
588/**
589 * resource_alignment - calculate resource's alignment
590 * @res: resource pointer
591 *
592 * Returns alignment on success, 0 (invalid alignment) on failure.
593 */
594resource_size_t resource_alignment(struct resource *res)
595{
596 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
597 case IORESOURCE_SIZEALIGN:
1a4e564b 598 return resource_size(res);
88452565
IK
599 case IORESOURCE_STARTALIGN:
600 return res->start;
601 default:
602 return 0;
603 }
604}
605
1da177e4
LT
606/*
607 * This is compatibility stuff for IO resources.
608 *
609 * Note how this, unlike the above, knows about
610 * the IO flag meanings (busy etc).
611 *
e1ca66d1 612 * request_region creates a new busy region.
1da177e4 613 *
e1ca66d1 614 * check_region returns non-zero if the area is already busy.
1da177e4 615 *
e1ca66d1
RD
616 * release_region releases a matching busy region.
617 */
618
619/**
620 * __request_region - create a new busy resource region
621 * @parent: parent resource descriptor
622 * @start: resource start address
623 * @n: resource region size
624 * @name: reserving caller's ID string
6ae301e8 625 * @flags: IO resource flags
1da177e4 626 */
d75fc8bb
GKH
627struct resource * __request_region(struct resource *parent,
628 resource_size_t start, resource_size_t n,
e8de1481 629 const char *name, int flags)
1da177e4 630{
dd392710 631 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
1da177e4 632
c26ec88e
BH
633 if (!res)
634 return NULL;
635
636 res->name = name;
637 res->start = start;
638 res->end = start + n - 1;
639 res->flags = IORESOURCE_BUSY;
e8de1481 640 res->flags |= flags;
c26ec88e
BH
641
642 write_lock(&resource_lock);
643
644 for (;;) {
645 struct resource *conflict;
646
647 conflict = __request_resource(parent, res);
648 if (!conflict)
1da177e4 649 break;
c26ec88e
BH
650 if (conflict != parent) {
651 parent = conflict;
652 if (!(conflict->flags & IORESOURCE_BUSY))
653 continue;
1da177e4 654 }
c26ec88e
BH
655
656 /* Uhhuh, that didn't work out.. */
657 kfree(res);
658 res = NULL;
659 break;
1da177e4 660 }
c26ec88e 661 write_unlock(&resource_lock);
1da177e4
LT
662 return res;
663}
1da177e4
LT
664EXPORT_SYMBOL(__request_region);
665
e1ca66d1
RD
666/**
667 * __check_region - check if a resource region is busy or free
668 * @parent: parent resource descriptor
669 * @start: resource start address
670 * @n: resource region size
671 *
672 * Returns 0 if the region is free at the moment it is checked,
673 * returns %-EBUSY if the region is busy.
674 *
675 * NOTE:
676 * This function is deprecated because its use is racy.
677 * Even if it returns 0, a subsequent call to request_region()
678 * may fail because another driver etc. just allocated the region.
679 * Do NOT use it. It will be removed from the kernel.
680 */
d75fc8bb
GKH
681int __check_region(struct resource *parent, resource_size_t start,
682 resource_size_t n)
1da177e4
LT
683{
684 struct resource * res;
685
e8de1481 686 res = __request_region(parent, start, n, "check-region", 0);
1da177e4
LT
687 if (!res)
688 return -EBUSY;
689
690 release_resource(res);
691 kfree(res);
692 return 0;
693}
1da177e4
LT
694EXPORT_SYMBOL(__check_region);
695
e1ca66d1
RD
696/**
697 * __release_region - release a previously reserved resource region
698 * @parent: parent resource descriptor
699 * @start: resource start address
700 * @n: resource region size
701 *
702 * The described resource region must match a currently busy region.
703 */
d75fc8bb
GKH
704void __release_region(struct resource *parent, resource_size_t start,
705 resource_size_t n)
1da177e4
LT
706{
707 struct resource **p;
d75fc8bb 708 resource_size_t end;
1da177e4
LT
709
710 p = &parent->child;
711 end = start + n - 1;
712
713 write_lock(&resource_lock);
714
715 for (;;) {
716 struct resource *res = *p;
717
718 if (!res)
719 break;
720 if (res->start <= start && res->end >= end) {
721 if (!(res->flags & IORESOURCE_BUSY)) {
722 p = &res->child;
723 continue;
724 }
725 if (res->start != start || res->end != end)
726 break;
727 *p = res->sibling;
728 write_unlock(&resource_lock);
729 kfree(res);
730 return;
731 }
732 p = &res->sibling;
733 }
734
735 write_unlock(&resource_lock);
736
685143ac
GKH
737 printk(KERN_WARNING "Trying to free nonexistent resource "
738 "<%016llx-%016llx>\n", (unsigned long long)start,
739 (unsigned long long)end);
1da177e4 740}
1da177e4
LT
741EXPORT_SYMBOL(__release_region);
742
9ac7849e
TH
743/*
744 * Managed region resource
745 */
746struct region_devres {
747 struct resource *parent;
748 resource_size_t start;
749 resource_size_t n;
750};
751
752static void devm_region_release(struct device *dev, void *res)
753{
754 struct region_devres *this = res;
755
756 __release_region(this->parent, this->start, this->n);
757}
758
759static int devm_region_match(struct device *dev, void *res, void *match_data)
760{
761 struct region_devres *this = res, *match = match_data;
762
763 return this->parent == match->parent &&
764 this->start == match->start && this->n == match->n;
765}
766
767struct resource * __devm_request_region(struct device *dev,
768 struct resource *parent, resource_size_t start,
769 resource_size_t n, const char *name)
770{
771 struct region_devres *dr = NULL;
772 struct resource *res;
773
774 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
775 GFP_KERNEL);
776 if (!dr)
777 return NULL;
778
779 dr->parent = parent;
780 dr->start = start;
781 dr->n = n;
782
e8de1481 783 res = __request_region(parent, start, n, name, 0);
9ac7849e
TH
784 if (res)
785 devres_add(dev, dr);
786 else
787 devres_free(dr);
788
789 return res;
790}
791EXPORT_SYMBOL(__devm_request_region);
792
793void __devm_release_region(struct device *dev, struct resource *parent,
794 resource_size_t start, resource_size_t n)
795{
796 struct region_devres match_data = { parent, start, n };
797
798 __release_region(parent, start, n);
799 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
800 &match_data));
801}
802EXPORT_SYMBOL(__devm_release_region);
803
1da177e4
LT
804/*
805 * Called from init/main.c to reserve IO ports.
806 */
807#define MAXRESERVE 4
808static int __init reserve_setup(char *str)
809{
810 static int reserved;
811 static struct resource reserve[MAXRESERVE];
812
813 for (;;) {
8bc1ad7d 814 unsigned int io_start, io_num;
1da177e4
LT
815 int x = reserved;
816
817 if (get_option (&str, &io_start) != 2)
818 break;
819 if (get_option (&str, &io_num) == 0)
820 break;
821 if (x < MAXRESERVE) {
822 struct resource *res = reserve + x;
823 res->name = "reserved";
824 res->start = io_start;
825 res->end = io_start + io_num - 1;
826 res->flags = IORESOURCE_BUSY;
827 res->child = NULL;
828 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
829 reserved = x+1;
830 }
831 }
832 return 1;
833}
834
835__setup("reserve=", reserve_setup);
379daf62
SS
836
837/*
838 * Check if the requested addr and size spans more than any slot in the
839 * iomem resource tree.
840 */
841int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
842{
843 struct resource *p = &iomem_resource;
844 int err = 0;
845 loff_t l;
846
847 read_lock(&resource_lock);
848 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
849 /*
850 * We can probably skip the resources without
851 * IORESOURCE_IO attribute?
852 */
853 if (p->start >= addr + size)
854 continue;
855 if (p->end < addr)
856 continue;
d68612b2
SS
857 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
858 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
379daf62 859 continue;
3ac52669
AV
860 /*
861 * if a resource is "BUSY", it's not a hardware resource
862 * but a driver mapping of such a resource; we don't want
863 * to warn for those; some drivers legitimately map only
864 * partial hardware resources. (example: vesafb)
865 */
866 if (p->flags & IORESOURCE_BUSY)
867 continue;
868
379daf62
SS
869 printk(KERN_WARNING "resource map sanity check conflict: "
870 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
13eb8375
IM
871 (unsigned long long)addr,
872 (unsigned long long)(addr + size - 1),
873 (unsigned long long)p->start,
874 (unsigned long long)p->end,
875 p->name);
379daf62
SS
876 err = -1;
877 break;
878 }
879 read_unlock(&resource_lock);
880
881 return err;
882}
e8de1481
AV
883
884#ifdef CONFIG_STRICT_DEVMEM
885static int strict_iomem_checks = 1;
886#else
887static int strict_iomem_checks;
888#endif
889
890/*
891 * check if an address is reserved in the iomem resource tree
892 * returns 1 if reserved, 0 if not reserved.
893 */
894int iomem_is_exclusive(u64 addr)
895{
896 struct resource *p = &iomem_resource;
897 int err = 0;
898 loff_t l;
899 int size = PAGE_SIZE;
900
901 if (!strict_iomem_checks)
902 return 0;
903
904 addr = addr & PAGE_MASK;
905
906 read_lock(&resource_lock);
907 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
908 /*
909 * We can probably skip the resources without
910 * IORESOURCE_IO attribute?
911 */
912 if (p->start >= addr + size)
913 break;
914 if (p->end < addr)
915 continue;
916 if (p->flags & IORESOURCE_BUSY &&
917 p->flags & IORESOURCE_EXCLUSIVE) {
918 err = 1;
919 break;
920 }
921 }
922 read_unlock(&resource_lock);
923
924 return err;
925}
926
927static int __init strict_iomem(char *str)
928{
929 if (strstr(str, "relaxed"))
930 strict_iomem_checks = 0;
931 if (strstr(str, "strict"))
932 strict_iomem_checks = 1;
933 return 1;
934}
935
936__setup("iomem=", strict_iomem);