]>
Commit | Line | Data |
---|---|---|
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 | ||
65fed8f6 OP |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
11 | ||
9984de1a | 12 | #include <linux/export.h> |
1da177e4 LT |
13 | #include <linux/errno.h> |
14 | #include <linux/ioport.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/spinlock.h> | |
18 | #include <linux/fs.h> | |
19 | #include <linux/proc_fs.h> | |
8b6d043b | 20 | #include <linux/sched.h> |
1da177e4 | 21 | #include <linux/seq_file.h> |
9ac7849e | 22 | #include <linux/device.h> |
d68612b2 | 23 | #include <linux/pfn.h> |
ebff7d8f | 24 | #include <linux/mm.h> |
1da177e4 LT |
25 | #include <asm/io.h> |
26 | ||
27 | ||
28 | struct resource ioport_resource = { | |
29 | .name = "PCI IO", | |
6550e07f | 30 | .start = 0, |
1da177e4 LT |
31 | .end = IO_SPACE_LIMIT, |
32 | .flags = IORESOURCE_IO, | |
33 | }; | |
1da177e4 LT |
34 | EXPORT_SYMBOL(ioport_resource); |
35 | ||
36 | struct resource iomem_resource = { | |
37 | .name = "PCI mem", | |
6550e07f GKH |
38 | .start = 0, |
39 | .end = -1, | |
1da177e4 LT |
40 | .flags = IORESOURCE_MEM, |
41 | }; | |
1da177e4 LT |
42 | EXPORT_SYMBOL(iomem_resource); |
43 | ||
23c570a6 RP |
44 | /* constraints to be met while allocating resources */ |
45 | struct resource_constraint { | |
46 | resource_size_t min, max, align; | |
47 | resource_size_t (*alignf)(void *, const struct resource *, | |
48 | resource_size_t, resource_size_t); | |
49 | void *alignf_data; | |
50 | }; | |
51 | ||
1da177e4 LT |
52 | static DEFINE_RWLOCK(resource_lock); |
53 | ||
ebff7d8f YI |
54 | /* |
55 | * For memory hotplug, there is no way to free resource entries allocated | |
56 | * by boot mem after the system is up. So for reusing the resource entry | |
57 | * we need to remember the resource. | |
58 | */ | |
59 | static struct resource *bootmem_resource_free; | |
60 | static DEFINE_SPINLOCK(bootmem_resource_lock); | |
61 | ||
1da177e4 LT |
62 | static void *r_next(struct seq_file *m, void *v, loff_t *pos) |
63 | { | |
64 | struct resource *p = v; | |
65 | (*pos)++; | |
66 | if (p->child) | |
67 | return p->child; | |
68 | while (!p->sibling && p->parent) | |
69 | p = p->parent; | |
70 | return p->sibling; | |
71 | } | |
72 | ||
13eb8375 IM |
73 | #ifdef CONFIG_PROC_FS |
74 | ||
75 | enum { MAX_IORES_LEVEL = 5 }; | |
76 | ||
1da177e4 LT |
77 | static void *r_start(struct seq_file *m, loff_t *pos) |
78 | __acquires(resource_lock) | |
79 | { | |
80 | struct resource *p = m->private; | |
81 | loff_t l = 0; | |
82 | read_lock(&resource_lock); | |
83 | for (p = p->child; p && l < *pos; p = r_next(m, p, &l)) | |
84 | ; | |
85 | return p; | |
86 | } | |
87 | ||
88 | static void r_stop(struct seq_file *m, void *v) | |
89 | __releases(resource_lock) | |
90 | { | |
91 | read_unlock(&resource_lock); | |
92 | } | |
93 | ||
94 | static int r_show(struct seq_file *m, void *v) | |
95 | { | |
96 | struct resource *root = m->private; | |
97 | struct resource *r = v, *p; | |
98 | int width = root->end < 0x10000 ? 4 : 8; | |
99 | int depth; | |
100 | ||
101 | for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent) | |
102 | if (p->parent == root) | |
103 | break; | |
685143ac | 104 | seq_printf(m, "%*s%0*llx-%0*llx : %s\n", |
1da177e4 | 105 | depth * 2, "", |
685143ac GKH |
106 | width, (unsigned long long) r->start, |
107 | width, (unsigned long long) r->end, | |
1da177e4 LT |
108 | r->name ? r->name : "<BAD>"); |
109 | return 0; | |
110 | } | |
111 | ||
15ad7cdc | 112 | static const struct seq_operations resource_op = { |
1da177e4 LT |
113 | .start = r_start, |
114 | .next = r_next, | |
115 | .stop = r_stop, | |
116 | .show = r_show, | |
117 | }; | |
118 | ||
119 | static int ioports_open(struct inode *inode, struct file *file) | |
120 | { | |
121 | int res = seq_open(file, &resource_op); | |
122 | if (!res) { | |
123 | struct seq_file *m = file->private_data; | |
124 | m->private = &ioport_resource; | |
125 | } | |
126 | return res; | |
127 | } | |
128 | ||
129 | static int iomem_open(struct inode *inode, struct file *file) | |
130 | { | |
131 | int res = seq_open(file, &resource_op); | |
132 | if (!res) { | |
133 | struct seq_file *m = file->private_data; | |
134 | m->private = &iomem_resource; | |
135 | } | |
136 | return res; | |
137 | } | |
138 | ||
15ad7cdc | 139 | static const struct file_operations proc_ioports_operations = { |
1da177e4 LT |
140 | .open = ioports_open, |
141 | .read = seq_read, | |
142 | .llseek = seq_lseek, | |
143 | .release = seq_release, | |
144 | }; | |
145 | ||
15ad7cdc | 146 | static const struct file_operations proc_iomem_operations = { |
1da177e4 LT |
147 | .open = iomem_open, |
148 | .read = seq_read, | |
149 | .llseek = seq_lseek, | |
150 | .release = seq_release, | |
151 | }; | |
152 | ||
153 | static int __init ioresources_init(void) | |
154 | { | |
c33fff0a DL |
155 | proc_create("ioports", 0, NULL, &proc_ioports_operations); |
156 | proc_create("iomem", 0, NULL, &proc_iomem_operations); | |
1da177e4 LT |
157 | return 0; |
158 | } | |
159 | __initcall(ioresources_init); | |
160 | ||
161 | #endif /* CONFIG_PROC_FS */ | |
162 | ||
ebff7d8f YI |
163 | static void free_resource(struct resource *res) |
164 | { | |
165 | if (!res) | |
166 | return; | |
167 | ||
168 | if (!PageSlab(virt_to_head_page(res))) { | |
169 | spin_lock(&bootmem_resource_lock); | |
170 | res->sibling = bootmem_resource_free; | |
171 | bootmem_resource_free = res; | |
172 | spin_unlock(&bootmem_resource_lock); | |
173 | } else { | |
174 | kfree(res); | |
175 | } | |
176 | } | |
177 | ||
178 | static struct resource *alloc_resource(gfp_t flags) | |
179 | { | |
180 | struct resource *res = NULL; | |
181 | ||
182 | spin_lock(&bootmem_resource_lock); | |
183 | if (bootmem_resource_free) { | |
184 | res = bootmem_resource_free; | |
185 | bootmem_resource_free = res->sibling; | |
186 | } | |
187 | spin_unlock(&bootmem_resource_lock); | |
188 | ||
189 | if (res) | |
190 | memset(res, 0, sizeof(struct resource)); | |
191 | else | |
192 | res = kzalloc(sizeof(struct resource), flags); | |
193 | ||
194 | return res; | |
195 | } | |
196 | ||
1da177e4 LT |
197 | /* Return the conflict entry if you can't request it */ |
198 | static struct resource * __request_resource(struct resource *root, struct resource *new) | |
199 | { | |
d75fc8bb GKH |
200 | resource_size_t start = new->start; |
201 | resource_size_t end = new->end; | |
1da177e4 LT |
202 | struct resource *tmp, **p; |
203 | ||
204 | if (end < start) | |
205 | return root; | |
206 | if (start < root->start) | |
207 | return root; | |
208 | if (end > root->end) | |
209 | return root; | |
210 | p = &root->child; | |
211 | for (;;) { | |
212 | tmp = *p; | |
213 | if (!tmp || tmp->start > end) { | |
214 | new->sibling = tmp; | |
215 | *p = new; | |
216 | new->parent = root; | |
217 | return NULL; | |
218 | } | |
219 | p = &tmp->sibling; | |
220 | if (tmp->end < start) | |
221 | continue; | |
222 | return tmp; | |
223 | } | |
224 | } | |
225 | ||
226 | static int __release_resource(struct resource *old) | |
227 | { | |
228 | struct resource *tmp, **p; | |
229 | ||
230 | p = &old->parent->child; | |
231 | for (;;) { | |
232 | tmp = *p; | |
233 | if (!tmp) | |
234 | break; | |
235 | if (tmp == old) { | |
236 | *p = tmp->sibling; | |
237 | old->parent = NULL; | |
238 | return 0; | |
239 | } | |
240 | p = &tmp->sibling; | |
241 | } | |
242 | return -EINVAL; | |
243 | } | |
244 | ||
5eeec0ec YL |
245 | static void __release_child_resources(struct resource *r) |
246 | { | |
247 | struct resource *tmp, *p; | |
248 | resource_size_t size; | |
249 | ||
250 | p = r->child; | |
251 | r->child = NULL; | |
252 | while (p) { | |
253 | tmp = p; | |
254 | p = p->sibling; | |
255 | ||
256 | tmp->parent = NULL; | |
257 | tmp->sibling = NULL; | |
258 | __release_child_resources(tmp); | |
259 | ||
260 | printk(KERN_DEBUG "release child resource %pR\n", tmp); | |
261 | /* need to restore size, and keep flags */ | |
262 | size = resource_size(tmp); | |
263 | tmp->start = 0; | |
264 | tmp->end = size - 1; | |
265 | } | |
266 | } | |
267 | ||
268 | void release_child_resources(struct resource *r) | |
269 | { | |
270 | write_lock(&resource_lock); | |
271 | __release_child_resources(r); | |
272 | write_unlock(&resource_lock); | |
273 | } | |
274 | ||
e1ca66d1 | 275 | /** |
66f1207b | 276 | * request_resource_conflict - request and reserve an I/O or memory resource |
e1ca66d1 RD |
277 | * @root: root resource descriptor |
278 | * @new: resource descriptor desired by caller | |
279 | * | |
66f1207b | 280 | * Returns 0 for success, conflict resource on error. |
e1ca66d1 | 281 | */ |
66f1207b | 282 | struct resource *request_resource_conflict(struct resource *root, struct resource *new) |
1da177e4 LT |
283 | { |
284 | struct resource *conflict; | |
285 | ||
286 | write_lock(&resource_lock); | |
287 | conflict = __request_resource(root, new); | |
288 | write_unlock(&resource_lock); | |
66f1207b BH |
289 | return conflict; |
290 | } | |
291 | ||
292 | /** | |
293 | * request_resource - request and reserve an I/O or memory resource | |
294 | * @root: root resource descriptor | |
295 | * @new: resource descriptor desired by caller | |
296 | * | |
297 | * Returns 0 for success, negative error code on error. | |
298 | */ | |
299 | int request_resource(struct resource *root, struct resource *new) | |
300 | { | |
301 | struct resource *conflict; | |
302 | ||
303 | conflict = request_resource_conflict(root, new); | |
1da177e4 LT |
304 | return conflict ? -EBUSY : 0; |
305 | } | |
306 | ||
307 | EXPORT_SYMBOL(request_resource); | |
308 | ||
e1ca66d1 RD |
309 | /** |
310 | * release_resource - release a previously reserved resource | |
311 | * @old: resource pointer | |
312 | */ | |
1da177e4 LT |
313 | int release_resource(struct resource *old) |
314 | { | |
315 | int retval; | |
316 | ||
317 | write_lock(&resource_lock); | |
318 | retval = __release_resource(old); | |
319 | write_unlock(&resource_lock); | |
320 | return retval; | |
321 | } | |
322 | ||
323 | EXPORT_SYMBOL(release_resource); | |
324 | ||
908eedc6 | 325 | #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY) |
2842f114 KH |
326 | /* |
327 | * Finds the lowest memory reosurce exists within [res->start.res->end) | |
908eedc6 | 328 | * the caller must specify res->start, res->end, res->flags and "name". |
2842f114 KH |
329 | * If found, returns 0, res is overwritten, if not found, returns -1. |
330 | */ | |
908eedc6 | 331 | static int find_next_system_ram(struct resource *res, char *name) |
2842f114 KH |
332 | { |
333 | resource_size_t start, end; | |
334 | struct resource *p; | |
335 | ||
336 | BUG_ON(!res); | |
337 | ||
338 | start = res->start; | |
339 | end = res->end; | |
58c1b5b0 | 340 | BUG_ON(start >= end); |
2842f114 KH |
341 | |
342 | read_lock(&resource_lock); | |
343 | for (p = iomem_resource.child; p ; p = p->sibling) { | |
344 | /* system ram is just marked as IORESOURCE_MEM */ | |
345 | if (p->flags != res->flags) | |
346 | continue; | |
908eedc6 KH |
347 | if (name && strcmp(p->name, name)) |
348 | continue; | |
2842f114 KH |
349 | if (p->start > end) { |
350 | p = NULL; | |
351 | break; | |
352 | } | |
58c1b5b0 | 353 | if ((p->end >= start) && (p->start < end)) |
2842f114 KH |
354 | break; |
355 | } | |
356 | read_unlock(&resource_lock); | |
357 | if (!p) | |
358 | return -1; | |
359 | /* copy data */ | |
0f04ab5e KH |
360 | if (res->start < p->start) |
361 | res->start = p->start; | |
362 | if (res->end > p->end) | |
363 | res->end = p->end; | |
2842f114 KH |
364 | return 0; |
365 | } | |
908eedc6 KH |
366 | |
367 | /* | |
368 | * This function calls callback against all memory range of "System RAM" | |
369 | * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY. | |
370 | * Now, this function is only for "System RAM". | |
371 | */ | |
372 | int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages, | |
373 | void *arg, int (*func)(unsigned long, unsigned long, void *)) | |
75884fb1 KH |
374 | { |
375 | struct resource res; | |
37b99dd5 | 376 | unsigned long pfn, end_pfn; |
75884fb1 KH |
377 | u64 orig_end; |
378 | int ret = -1; | |
908eedc6 | 379 | |
75884fb1 KH |
380 | res.start = (u64) start_pfn << PAGE_SHIFT; |
381 | res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1; | |
887c3cb1 | 382 | res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
75884fb1 | 383 | orig_end = res.end; |
908eedc6 KH |
384 | while ((res.start < res.end) && |
385 | (find_next_system_ram(&res, "System RAM") >= 0)) { | |
37b99dd5 WF |
386 | pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT; |
387 | end_pfn = (res.end + 1) >> PAGE_SHIFT; | |
388 | if (end_pfn > pfn) | |
f4149660 | 389 | ret = (*func)(pfn, end_pfn - pfn, arg); |
75884fb1 KH |
390 | if (ret) |
391 | break; | |
392 | res.start = res.end + 1; | |
393 | res.end = orig_end; | |
394 | } | |
395 | return ret; | |
396 | } | |
397 | ||
2842f114 KH |
398 | #endif |
399 | ||
61ef2489 WF |
400 | static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg) |
401 | { | |
402 | return 1; | |
403 | } | |
404 | /* | |
405 | * This generic page_is_ram() returns true if specified address is | |
406 | * registered as "System RAM" in iomem_resource list. | |
407 | */ | |
e5273007 | 408 | int __weak page_is_ram(unsigned long pfn) |
61ef2489 WF |
409 | { |
410 | return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1; | |
411 | } | |
c5a13032 | 412 | EXPORT_SYMBOL_GPL(page_is_ram); |
61ef2489 | 413 | |
fcb11918 BH |
414 | void __weak arch_remove_reservations(struct resource *avail) |
415 | { | |
416 | } | |
417 | ||
a9cea017 BH |
418 | static resource_size_t simple_align_resource(void *data, |
419 | const struct resource *avail, | |
420 | resource_size_t size, | |
421 | resource_size_t align) | |
422 | { | |
423 | return avail->start; | |
424 | } | |
425 | ||
5d6b1fa3 BH |
426 | static void resource_clip(struct resource *res, resource_size_t min, |
427 | resource_size_t max) | |
428 | { | |
429 | if (res->start < min) | |
430 | res->start = min; | |
431 | if (res->end > max) | |
432 | res->end = max; | |
433 | } | |
434 | ||
1da177e4 | 435 | /* |
23c570a6 RP |
436 | * Find empty slot in the resource tree with the given range and |
437 | * alignment constraints | |
1da177e4 | 438 | */ |
23c570a6 RP |
439 | static int __find_resource(struct resource *root, struct resource *old, |
440 | struct resource *new, | |
441 | resource_size_t size, | |
442 | struct resource_constraint *constraint) | |
1da177e4 LT |
443 | { |
444 | struct resource *this = root->child; | |
a1862e31 | 445 | struct resource tmp = *new, avail, alloc; |
1da177e4 | 446 | |
0e2c8b8f | 447 | tmp.start = root->start; |
1da177e4 | 448 | /* |
c0f5ac54 BH |
449 | * Skip past an allocated resource that starts at 0, since the assignment |
450 | * of this->start - 1 to tmp->end below would cause an underflow. | |
1da177e4 | 451 | */ |
23c570a6 RP |
452 | if (this && this->start == root->start) { |
453 | tmp.start = (this == old) ? old->start : this->end + 1; | |
1da177e4 LT |
454 | this = this->sibling; |
455 | } | |
c0f5ac54 | 456 | for(;;) { |
1da177e4 | 457 | if (this) |
23c570a6 | 458 | tmp.end = (this == old) ? this->end : this->start - 1; |
1da177e4 | 459 | else |
0e2c8b8f | 460 | tmp.end = root->end; |
5d6b1fa3 | 461 | |
47ea91b4 RP |
462 | if (tmp.end < tmp.start) |
463 | goto next; | |
464 | ||
23c570a6 | 465 | resource_clip(&tmp, constraint->min, constraint->max); |
fcb11918 | 466 | arch_remove_reservations(&tmp); |
a9cea017 | 467 | |
a1862e31 | 468 | /* Check for overflow after ALIGN() */ |
23c570a6 | 469 | avail.start = ALIGN(tmp.start, constraint->align); |
a1862e31 | 470 | avail.end = tmp.end; |
5edb93b8 | 471 | avail.flags = new->flags & ~IORESOURCE_UNSET; |
a1862e31 | 472 | if (avail.start >= tmp.start) { |
5edb93b8 | 473 | alloc.flags = avail.flags; |
23c570a6 RP |
474 | alloc.start = constraint->alignf(constraint->alignf_data, &avail, |
475 | size, constraint->align); | |
a1862e31 | 476 | alloc.end = alloc.start + size - 1; |
8e8c0ed1 TI |
477 | if (alloc.start <= alloc.end && |
478 | resource_contains(&avail, &alloc)) { | |
a1862e31 BH |
479 | new->start = alloc.start; |
480 | new->end = alloc.end; | |
481 | return 0; | |
482 | } | |
1da177e4 | 483 | } |
47ea91b4 RP |
484 | |
485 | next: if (!this || this->end == root->end) | |
1da177e4 | 486 | break; |
47ea91b4 | 487 | |
23c570a6 RP |
488 | if (this != old) |
489 | tmp.start = this->end + 1; | |
1da177e4 LT |
490 | this = this->sibling; |
491 | } | |
492 | return -EBUSY; | |
493 | } | |
494 | ||
23c570a6 RP |
495 | /* |
496 | * Find empty slot in the resource tree given range and alignment. | |
497 | */ | |
498 | static int find_resource(struct resource *root, struct resource *new, | |
499 | resource_size_t size, | |
500 | struct resource_constraint *constraint) | |
501 | { | |
502 | return __find_resource(root, NULL, new, size, constraint); | |
503 | } | |
504 | ||
e1ca66d1 | 505 | /** |
23c570a6 RP |
506 | * reallocate_resource - allocate a slot in the resource tree given range & alignment. |
507 | * The resource will be relocated if the new size cannot be reallocated in the | |
508 | * current location. | |
509 | * | |
510 | * @root: root resource descriptor | |
511 | * @old: resource descriptor desired by caller | |
512 | * @newsize: new size of the resource descriptor | |
513 | * @constraint: the size and alignment constraints to be met. | |
514 | */ | |
28ab49ff | 515 | static int reallocate_resource(struct resource *root, struct resource *old, |
23c570a6 RP |
516 | resource_size_t newsize, |
517 | struct resource_constraint *constraint) | |
518 | { | |
519 | int err=0; | |
520 | struct resource new = *old; | |
521 | struct resource *conflict; | |
522 | ||
523 | write_lock(&resource_lock); | |
524 | ||
525 | if ((err = __find_resource(root, old, &new, newsize, constraint))) | |
526 | goto out; | |
527 | ||
528 | if (resource_contains(&new, old)) { | |
529 | old->start = new.start; | |
530 | old->end = new.end; | |
531 | goto out; | |
532 | } | |
533 | ||
534 | if (old->child) { | |
535 | err = -EBUSY; | |
536 | goto out; | |
537 | } | |
538 | ||
539 | if (resource_contains(old, &new)) { | |
540 | old->start = new.start; | |
541 | old->end = new.end; | |
542 | } else { | |
543 | __release_resource(old); | |
544 | *old = new; | |
545 | conflict = __request_resource(root, old); | |
546 | BUG_ON(conflict); | |
547 | } | |
548 | out: | |
549 | write_unlock(&resource_lock); | |
550 | return err; | |
551 | } | |
552 | ||
553 | ||
554 | /** | |
555 | * allocate_resource - allocate empty slot in the resource tree given range & alignment. | |
556 | * The resource will be reallocated with a new size if it was already allocated | |
e1ca66d1 RD |
557 | * @root: root resource descriptor |
558 | * @new: resource descriptor desired by caller | |
559 | * @size: requested resource region size | |
ee5e5683 WY |
560 | * @min: minimum boundary to allocate |
561 | * @max: maximum boundary to allocate | |
e1ca66d1 RD |
562 | * @align: alignment requested, in bytes |
563 | * @alignf: alignment function, optional, called if not NULL | |
564 | * @alignf_data: arbitrary data to pass to the @alignf function | |
1da177e4 LT |
565 | */ |
566 | int allocate_resource(struct resource *root, struct resource *new, | |
d75fc8bb GKH |
567 | resource_size_t size, resource_size_t min, |
568 | resource_size_t max, resource_size_t align, | |
b26b2d49 | 569 | resource_size_t (*alignf)(void *, |
3b7a17fc | 570 | const struct resource *, |
b26b2d49 DB |
571 | resource_size_t, |
572 | resource_size_t), | |
1da177e4 LT |
573 | void *alignf_data) |
574 | { | |
575 | int err; | |
23c570a6 | 576 | struct resource_constraint constraint; |
1da177e4 | 577 | |
a9cea017 BH |
578 | if (!alignf) |
579 | alignf = simple_align_resource; | |
580 | ||
23c570a6 RP |
581 | constraint.min = min; |
582 | constraint.max = max; | |
583 | constraint.align = align; | |
584 | constraint.alignf = alignf; | |
585 | constraint.alignf_data = alignf_data; | |
586 | ||
587 | if ( new->parent ) { | |
588 | /* resource is already allocated, try reallocating with | |
589 | the new constraints */ | |
590 | return reallocate_resource(root, new, size, &constraint); | |
591 | } | |
592 | ||
1da177e4 | 593 | write_lock(&resource_lock); |
23c570a6 | 594 | err = find_resource(root, new, size, &constraint); |
1da177e4 LT |
595 | if (err >= 0 && __request_resource(root, new)) |
596 | err = -EBUSY; | |
597 | write_unlock(&resource_lock); | |
598 | return err; | |
599 | } | |
600 | ||
601 | EXPORT_SYMBOL(allocate_resource); | |
602 | ||
1c388919 GU |
603 | /** |
604 | * lookup_resource - find an existing resource by a resource start address | |
605 | * @root: root resource descriptor | |
606 | * @start: resource start address | |
607 | * | |
608 | * Returns a pointer to the resource if found, NULL otherwise | |
609 | */ | |
610 | struct resource *lookup_resource(struct resource *root, resource_size_t start) | |
611 | { | |
612 | struct resource *res; | |
613 | ||
614 | read_lock(&resource_lock); | |
615 | for (res = root->child; res; res = res->sibling) { | |
616 | if (res->start == start) | |
617 | break; | |
618 | } | |
619 | read_unlock(&resource_lock); | |
620 | ||
621 | return res; | |
622 | } | |
623 | ||
bef69ea0 LT |
624 | /* |
625 | * Insert a resource into the resource tree. If successful, return NULL, | |
626 | * otherwise return the conflicting resource (compare to __request_resource()) | |
1da177e4 | 627 | */ |
bef69ea0 | 628 | static struct resource * __insert_resource(struct resource *parent, struct resource *new) |
1da177e4 | 629 | { |
1da177e4 LT |
630 | struct resource *first, *next; |
631 | ||
d33b6fba | 632 | for (;; parent = first) { |
d33b6fba MW |
633 | first = __request_resource(parent, new); |
634 | if (!first) | |
bef69ea0 | 635 | return first; |
d33b6fba | 636 | |
d33b6fba | 637 | if (first == parent) |
bef69ea0 | 638 | return first; |
5de1cb2d HS |
639 | if (WARN_ON(first == new)) /* duplicated insertion */ |
640 | return first; | |
d33b6fba MW |
641 | |
642 | if ((first->start > new->start) || (first->end < new->end)) | |
643 | break; | |
644 | if ((first->start == new->start) && (first->end == new->end)) | |
645 | break; | |
1da177e4 LT |
646 | } |
647 | ||
648 | for (next = first; ; next = next->sibling) { | |
649 | /* Partial overlap? Bad, and unfixable */ | |
650 | if (next->start < new->start || next->end > new->end) | |
bef69ea0 | 651 | return next; |
1da177e4 LT |
652 | if (!next->sibling) |
653 | break; | |
654 | if (next->sibling->start > new->end) | |
655 | break; | |
656 | } | |
657 | ||
1da177e4 LT |
658 | new->parent = parent; |
659 | new->sibling = next->sibling; | |
660 | new->child = first; | |
661 | ||
662 | next->sibling = NULL; | |
663 | for (next = first; next; next = next->sibling) | |
664 | next->parent = new; | |
665 | ||
666 | if (parent->child == first) { | |
667 | parent->child = new; | |
668 | } else { | |
669 | next = parent->child; | |
670 | while (next->sibling != first) | |
671 | next = next->sibling; | |
672 | next->sibling = new; | |
673 | } | |
bef69ea0 LT |
674 | return NULL; |
675 | } | |
1da177e4 | 676 | |
bef69ea0 | 677 | /** |
66f1207b | 678 | * insert_resource_conflict - Inserts resource in the resource tree |
bef69ea0 LT |
679 | * @parent: parent of the new resource |
680 | * @new: new resource to insert | |
681 | * | |
66f1207b | 682 | * Returns 0 on success, conflict resource if the resource can't be inserted. |
bef69ea0 | 683 | * |
66f1207b | 684 | * This function is equivalent to request_resource_conflict when no conflict |
bef69ea0 LT |
685 | * happens. If a conflict happens, and the conflicting resources |
686 | * entirely fit within the range of the new resource, then the new | |
687 | * resource is inserted and the conflicting resources become children of | |
688 | * the new resource. | |
689 | */ | |
66f1207b | 690 | struct resource *insert_resource_conflict(struct resource *parent, struct resource *new) |
bef69ea0 LT |
691 | { |
692 | struct resource *conflict; | |
693 | ||
694 | write_lock(&resource_lock); | |
695 | conflict = __insert_resource(parent, new); | |
696 | write_unlock(&resource_lock); | |
66f1207b BH |
697 | return conflict; |
698 | } | |
699 | ||
700 | /** | |
701 | * insert_resource - Inserts a resource in the resource tree | |
702 | * @parent: parent of the new resource | |
703 | * @new: new resource to insert | |
704 | * | |
705 | * Returns 0 on success, -EBUSY if the resource can't be inserted. | |
706 | */ | |
707 | int insert_resource(struct resource *parent, struct resource *new) | |
708 | { | |
709 | struct resource *conflict; | |
710 | ||
711 | conflict = insert_resource_conflict(parent, new); | |
bef69ea0 LT |
712 | return conflict ? -EBUSY : 0; |
713 | } | |
714 | ||
715 | /** | |
716 | * insert_resource_expand_to_fit - Insert a resource into the resource tree | |
6781f4ae | 717 | * @root: root resource descriptor |
bef69ea0 LT |
718 | * @new: new resource to insert |
719 | * | |
720 | * Insert a resource into the resource tree, possibly expanding it in order | |
721 | * to make it encompass any conflicting resources. | |
722 | */ | |
723 | void insert_resource_expand_to_fit(struct resource *root, struct resource *new) | |
724 | { | |
725 | if (new->parent) | |
726 | return; | |
727 | ||
728 | write_lock(&resource_lock); | |
729 | for (;;) { | |
730 | struct resource *conflict; | |
731 | ||
732 | conflict = __insert_resource(root, new); | |
733 | if (!conflict) | |
734 | break; | |
735 | if (conflict == root) | |
736 | break; | |
737 | ||
738 | /* Ok, expand resource to cover the conflict, then try again .. */ | |
739 | if (conflict->start < new->start) | |
740 | new->start = conflict->start; | |
741 | if (conflict->end > new->end) | |
742 | new->end = conflict->end; | |
743 | ||
744 | printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name); | |
745 | } | |
1da177e4 | 746 | write_unlock(&resource_lock); |
1da177e4 LT |
747 | } |
748 | ||
ae8e3a91 TK |
749 | static int __adjust_resource(struct resource *res, resource_size_t start, |
750 | resource_size_t size) | |
1da177e4 LT |
751 | { |
752 | struct resource *tmp, *parent = res->parent; | |
d75fc8bb | 753 | resource_size_t end = start + size - 1; |
1da177e4 LT |
754 | int result = -EBUSY; |
755 | ||
82ec90ea YL |
756 | if (!parent) |
757 | goto skip; | |
758 | ||
1da177e4 LT |
759 | if ((start < parent->start) || (end > parent->end)) |
760 | goto out; | |
761 | ||
1da177e4 LT |
762 | if (res->sibling && (res->sibling->start <= end)) |
763 | goto out; | |
764 | ||
765 | tmp = parent->child; | |
766 | if (tmp != res) { | |
767 | while (tmp->sibling != res) | |
768 | tmp = tmp->sibling; | |
769 | if (start <= tmp->end) | |
770 | goto out; | |
771 | } | |
772 | ||
82ec90ea YL |
773 | skip: |
774 | for (tmp = res->child; tmp; tmp = tmp->sibling) | |
775 | if ((tmp->start < start) || (tmp->end > end)) | |
776 | goto out; | |
777 | ||
1da177e4 LT |
778 | res->start = start; |
779 | res->end = end; | |
780 | result = 0; | |
781 | ||
782 | out: | |
ae8e3a91 TK |
783 | return result; |
784 | } | |
785 | ||
786 | /** | |
787 | * adjust_resource - modify a resource's start and size | |
788 | * @res: resource to modify | |
789 | * @start: new start value | |
790 | * @size: new size | |
791 | * | |
792 | * Given an existing resource, change its start and size to match the | |
793 | * arguments. Returns 0 on success, -EBUSY if it can't fit. | |
794 | * Existing children of the resource are assumed to be immutable. | |
795 | */ | |
796 | int adjust_resource(struct resource *res, resource_size_t start, | |
797 | resource_size_t size) | |
798 | { | |
799 | int result; | |
800 | ||
801 | write_lock(&resource_lock); | |
802 | result = __adjust_resource(res, start, size); | |
1da177e4 LT |
803 | write_unlock(&resource_lock); |
804 | return result; | |
805 | } | |
24105748 | 806 | EXPORT_SYMBOL(adjust_resource); |
1da177e4 | 807 | |
268364a0 YL |
808 | static void __init __reserve_region_with_split(struct resource *root, |
809 | resource_size_t start, resource_size_t end, | |
810 | const char *name) | |
811 | { | |
812 | struct resource *parent = root; | |
813 | struct resource *conflict; | |
ebff7d8f | 814 | struct resource *res = alloc_resource(GFP_ATOMIC); |
4965f566 | 815 | struct resource *next_res = NULL; |
268364a0 YL |
816 | |
817 | if (!res) | |
818 | return; | |
819 | ||
820 | res->name = name; | |
821 | res->start = start; | |
822 | res->end = end; | |
823 | res->flags = IORESOURCE_BUSY; | |
824 | ||
4965f566 | 825 | while (1) { |
268364a0 | 826 | |
4965f566 M |
827 | conflict = __request_resource(parent, res); |
828 | if (!conflict) { | |
829 | if (!next_res) | |
830 | break; | |
831 | res = next_res; | |
832 | next_res = NULL; | |
833 | continue; | |
834 | } | |
268364a0 | 835 | |
4965f566 M |
836 | /* conflict covered whole area */ |
837 | if (conflict->start <= res->start && | |
838 | conflict->end >= res->end) { | |
ebff7d8f | 839 | free_resource(res); |
4965f566 M |
840 | WARN_ON(next_res); |
841 | break; | |
842 | } | |
843 | ||
844 | /* failed, split and try again */ | |
845 | if (conflict->start > res->start) { | |
846 | end = res->end; | |
847 | res->end = conflict->start - 1; | |
848 | if (conflict->end < end) { | |
ebff7d8f | 849 | next_res = alloc_resource(GFP_ATOMIC); |
4965f566 | 850 | if (!next_res) { |
ebff7d8f | 851 | free_resource(res); |
4965f566 M |
852 | break; |
853 | } | |
854 | next_res->name = name; | |
855 | next_res->start = conflict->end + 1; | |
856 | next_res->end = end; | |
857 | next_res->flags = IORESOURCE_BUSY; | |
858 | } | |
859 | } else { | |
860 | res->start = conflict->end + 1; | |
861 | } | |
862 | } | |
268364a0 YL |
863 | |
864 | } | |
865 | ||
bea92112 | 866 | void __init reserve_region_with_split(struct resource *root, |
268364a0 YL |
867 | resource_size_t start, resource_size_t end, |
868 | const char *name) | |
869 | { | |
65fed8f6 OP |
870 | int abort = 0; |
871 | ||
268364a0 | 872 | write_lock(&resource_lock); |
65fed8f6 OP |
873 | if (root->start > start || root->end < end) { |
874 | pr_err("requested range [0x%llx-0x%llx] not in root %pr\n", | |
875 | (unsigned long long)start, (unsigned long long)end, | |
876 | root); | |
877 | if (start > root->end || end < root->start) | |
878 | abort = 1; | |
879 | else { | |
880 | if (end > root->end) | |
881 | end = root->end; | |
882 | if (start < root->start) | |
883 | start = root->start; | |
884 | pr_err("fixing request to [0x%llx-0x%llx]\n", | |
885 | (unsigned long long)start, | |
886 | (unsigned long long)end); | |
887 | } | |
888 | dump_stack(); | |
889 | } | |
890 | if (!abort) | |
891 | __reserve_region_with_split(root, start, end, name); | |
268364a0 YL |
892 | write_unlock(&resource_lock); |
893 | } | |
894 | ||
88452565 IK |
895 | /** |
896 | * resource_alignment - calculate resource's alignment | |
897 | * @res: resource pointer | |
898 | * | |
899 | * Returns alignment on success, 0 (invalid alignment) on failure. | |
900 | */ | |
901 | resource_size_t resource_alignment(struct resource *res) | |
902 | { | |
903 | switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) { | |
904 | case IORESOURCE_SIZEALIGN: | |
1a4e564b | 905 | return resource_size(res); |
88452565 IK |
906 | case IORESOURCE_STARTALIGN: |
907 | return res->start; | |
908 | default: | |
909 | return 0; | |
910 | } | |
911 | } | |
912 | ||
1da177e4 LT |
913 | /* |
914 | * This is compatibility stuff for IO resources. | |
915 | * | |
916 | * Note how this, unlike the above, knows about | |
917 | * the IO flag meanings (busy etc). | |
918 | * | |
e1ca66d1 | 919 | * request_region creates a new busy region. |
1da177e4 | 920 | * |
e1ca66d1 | 921 | * check_region returns non-zero if the area is already busy. |
1da177e4 | 922 | * |
e1ca66d1 RD |
923 | * release_region releases a matching busy region. |
924 | */ | |
925 | ||
8b6d043b AC |
926 | static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait); |
927 | ||
e1ca66d1 RD |
928 | /** |
929 | * __request_region - create a new busy resource region | |
930 | * @parent: parent resource descriptor | |
931 | * @start: resource start address | |
932 | * @n: resource region size | |
933 | * @name: reserving caller's ID string | |
6ae301e8 | 934 | * @flags: IO resource flags |
1da177e4 | 935 | */ |
d75fc8bb GKH |
936 | struct resource * __request_region(struct resource *parent, |
937 | resource_size_t start, resource_size_t n, | |
e8de1481 | 938 | const char *name, int flags) |
1da177e4 | 939 | { |
8b6d043b | 940 | DECLARE_WAITQUEUE(wait, current); |
ebff7d8f | 941 | struct resource *res = alloc_resource(GFP_KERNEL); |
1da177e4 | 942 | |
c26ec88e BH |
943 | if (!res) |
944 | return NULL; | |
945 | ||
946 | res->name = name; | |
947 | res->start = start; | |
948 | res->end = start + n - 1; | |
6404e88e BH |
949 | res->flags = resource_type(parent); |
950 | res->flags |= IORESOURCE_BUSY | flags; | |
c26ec88e BH |
951 | |
952 | write_lock(&resource_lock); | |
953 | ||
954 | for (;;) { | |
955 | struct resource *conflict; | |
956 | ||
957 | conflict = __request_resource(parent, res); | |
958 | if (!conflict) | |
1da177e4 | 959 | break; |
c26ec88e | 960 | if (conflict != parent) { |
c40374d5 SG |
961 | if (!(conflict->flags & IORESOURCE_BUSY)) { |
962 | parent = conflict; | |
c26ec88e | 963 | continue; |
c40374d5 | 964 | } |
1da177e4 | 965 | } |
8b6d043b AC |
966 | if (conflict->flags & flags & IORESOURCE_MUXED) { |
967 | add_wait_queue(&muxed_resource_wait, &wait); | |
968 | write_unlock(&resource_lock); | |
969 | set_current_state(TASK_UNINTERRUPTIBLE); | |
970 | schedule(); | |
971 | remove_wait_queue(&muxed_resource_wait, &wait); | |
972 | write_lock(&resource_lock); | |
973 | continue; | |
974 | } | |
c26ec88e | 975 | /* Uhhuh, that didn't work out.. */ |
ebff7d8f | 976 | free_resource(res); |
c26ec88e BH |
977 | res = NULL; |
978 | break; | |
1da177e4 | 979 | } |
c26ec88e | 980 | write_unlock(&resource_lock); |
1da177e4 LT |
981 | return res; |
982 | } | |
1da177e4 LT |
983 | EXPORT_SYMBOL(__request_region); |
984 | ||
e1ca66d1 RD |
985 | /** |
986 | * __check_region - check if a resource region is busy or free | |
987 | * @parent: parent resource descriptor | |
988 | * @start: resource start address | |
989 | * @n: resource region size | |
990 | * | |
991 | * Returns 0 if the region is free at the moment it is checked, | |
992 | * returns %-EBUSY if the region is busy. | |
993 | * | |
994 | * NOTE: | |
995 | * This function is deprecated because its use is racy. | |
996 | * Even if it returns 0, a subsequent call to request_region() | |
997 | * may fail because another driver etc. just allocated the region. | |
998 | * Do NOT use it. It will be removed from the kernel. | |
999 | */ | |
d75fc8bb GKH |
1000 | int __check_region(struct resource *parent, resource_size_t start, |
1001 | resource_size_t n) | |
1da177e4 LT |
1002 | { |
1003 | struct resource * res; | |
1004 | ||
e8de1481 | 1005 | res = __request_region(parent, start, n, "check-region", 0); |
1da177e4 LT |
1006 | if (!res) |
1007 | return -EBUSY; | |
1008 | ||
1009 | release_resource(res); | |
ebff7d8f | 1010 | free_resource(res); |
1da177e4 LT |
1011 | return 0; |
1012 | } | |
1da177e4 LT |
1013 | EXPORT_SYMBOL(__check_region); |
1014 | ||
e1ca66d1 RD |
1015 | /** |
1016 | * __release_region - release a previously reserved resource region | |
1017 | * @parent: parent resource descriptor | |
1018 | * @start: resource start address | |
1019 | * @n: resource region size | |
1020 | * | |
1021 | * The described resource region must match a currently busy region. | |
1022 | */ | |
d75fc8bb GKH |
1023 | void __release_region(struct resource *parent, resource_size_t start, |
1024 | resource_size_t n) | |
1da177e4 LT |
1025 | { |
1026 | struct resource **p; | |
d75fc8bb | 1027 | resource_size_t end; |
1da177e4 LT |
1028 | |
1029 | p = &parent->child; | |
1030 | end = start + n - 1; | |
1031 | ||
1032 | write_lock(&resource_lock); | |
1033 | ||
1034 | for (;;) { | |
1035 | struct resource *res = *p; | |
1036 | ||
1037 | if (!res) | |
1038 | break; | |
1039 | if (res->start <= start && res->end >= end) { | |
1040 | if (!(res->flags & IORESOURCE_BUSY)) { | |
1041 | p = &res->child; | |
1042 | continue; | |
1043 | } | |
1044 | if (res->start != start || res->end != end) | |
1045 | break; | |
1046 | *p = res->sibling; | |
1047 | write_unlock(&resource_lock); | |
8b6d043b AC |
1048 | if (res->flags & IORESOURCE_MUXED) |
1049 | wake_up(&muxed_resource_wait); | |
ebff7d8f | 1050 | free_resource(res); |
1da177e4 LT |
1051 | return; |
1052 | } | |
1053 | p = &res->sibling; | |
1054 | } | |
1055 | ||
1056 | write_unlock(&resource_lock); | |
1057 | ||
685143ac GKH |
1058 | printk(KERN_WARNING "Trying to free nonexistent resource " |
1059 | "<%016llx-%016llx>\n", (unsigned long long)start, | |
1060 | (unsigned long long)end); | |
1da177e4 | 1061 | } |
1da177e4 LT |
1062 | EXPORT_SYMBOL(__release_region); |
1063 | ||
825f787b TK |
1064 | #ifdef CONFIG_MEMORY_HOTREMOVE |
1065 | /** | |
1066 | * release_mem_region_adjustable - release a previously reserved memory region | |
1067 | * @parent: parent resource descriptor | |
1068 | * @start: resource start address | |
1069 | * @size: resource region size | |
1070 | * | |
1071 | * This interface is intended for memory hot-delete. The requested region | |
1072 | * is released from a currently busy memory resource. The requested region | |
1073 | * must either match exactly or fit into a single busy resource entry. In | |
1074 | * the latter case, the remaining resource is adjusted accordingly. | |
1075 | * Existing children of the busy memory resource must be immutable in the | |
1076 | * request. | |
1077 | * | |
1078 | * Note: | |
1079 | * - Additional release conditions, such as overlapping region, can be | |
1080 | * supported after they are confirmed as valid cases. | |
1081 | * - When a busy memory resource gets split into two entries, the code | |
1082 | * assumes that all children remain in the lower address entry for | |
1083 | * simplicity. Enhance this logic when necessary. | |
1084 | */ | |
1085 | int release_mem_region_adjustable(struct resource *parent, | |
1086 | resource_size_t start, resource_size_t size) | |
1087 | { | |
1088 | struct resource **p; | |
1089 | struct resource *res; | |
1090 | struct resource *new_res; | |
1091 | resource_size_t end; | |
1092 | int ret = -EINVAL; | |
1093 | ||
1094 | end = start + size - 1; | |
1095 | if ((start < parent->start) || (end > parent->end)) | |
1096 | return ret; | |
1097 | ||
ebff7d8f YI |
1098 | /* The alloc_resource() result gets checked later */ |
1099 | new_res = alloc_resource(GFP_KERNEL); | |
825f787b TK |
1100 | |
1101 | p = &parent->child; | |
1102 | write_lock(&resource_lock); | |
1103 | ||
1104 | while ((res = *p)) { | |
1105 | if (res->start >= end) | |
1106 | break; | |
1107 | ||
1108 | /* look for the next resource if it does not fit into */ | |
1109 | if (res->start > start || res->end < end) { | |
1110 | p = &res->sibling; | |
1111 | continue; | |
1112 | } | |
1113 | ||
1114 | if (!(res->flags & IORESOURCE_MEM)) | |
1115 | break; | |
1116 | ||
1117 | if (!(res->flags & IORESOURCE_BUSY)) { | |
1118 | p = &res->child; | |
1119 | continue; | |
1120 | } | |
1121 | ||
1122 | /* found the target resource; let's adjust accordingly */ | |
1123 | if (res->start == start && res->end == end) { | |
1124 | /* free the whole entry */ | |
1125 | *p = res->sibling; | |
ebff7d8f | 1126 | free_resource(res); |
825f787b TK |
1127 | ret = 0; |
1128 | } else if (res->start == start && res->end != end) { | |
1129 | /* adjust the start */ | |
1130 | ret = __adjust_resource(res, end + 1, | |
1131 | res->end - end); | |
1132 | } else if (res->start != start && res->end == end) { | |
1133 | /* adjust the end */ | |
1134 | ret = __adjust_resource(res, res->start, | |
1135 | start - res->start); | |
1136 | } else { | |
1137 | /* split into two entries */ | |
1138 | if (!new_res) { | |
1139 | ret = -ENOMEM; | |
1140 | break; | |
1141 | } | |
1142 | new_res->name = res->name; | |
1143 | new_res->start = end + 1; | |
1144 | new_res->end = res->end; | |
1145 | new_res->flags = res->flags; | |
1146 | new_res->parent = res->parent; | |
1147 | new_res->sibling = res->sibling; | |
1148 | new_res->child = NULL; | |
1149 | ||
1150 | ret = __adjust_resource(res, res->start, | |
1151 | start - res->start); | |
1152 | if (ret) | |
1153 | break; | |
1154 | res->sibling = new_res; | |
1155 | new_res = NULL; | |
1156 | } | |
1157 | ||
1158 | break; | |
1159 | } | |
1160 | ||
1161 | write_unlock(&resource_lock); | |
ebff7d8f | 1162 | free_resource(new_res); |
825f787b TK |
1163 | return ret; |
1164 | } | |
1165 | #endif /* CONFIG_MEMORY_HOTREMOVE */ | |
1166 | ||
9ac7849e TH |
1167 | /* |
1168 | * Managed region resource | |
1169 | */ | |
1170 | struct region_devres { | |
1171 | struct resource *parent; | |
1172 | resource_size_t start; | |
1173 | resource_size_t n; | |
1174 | }; | |
1175 | ||
1176 | static void devm_region_release(struct device *dev, void *res) | |
1177 | { | |
1178 | struct region_devres *this = res; | |
1179 | ||
1180 | __release_region(this->parent, this->start, this->n); | |
1181 | } | |
1182 | ||
1183 | static int devm_region_match(struct device *dev, void *res, void *match_data) | |
1184 | { | |
1185 | struct region_devres *this = res, *match = match_data; | |
1186 | ||
1187 | return this->parent == match->parent && | |
1188 | this->start == match->start && this->n == match->n; | |
1189 | } | |
1190 | ||
1191 | struct resource * __devm_request_region(struct device *dev, | |
1192 | struct resource *parent, resource_size_t start, | |
1193 | resource_size_t n, const char *name) | |
1194 | { | |
1195 | struct region_devres *dr = NULL; | |
1196 | struct resource *res; | |
1197 | ||
1198 | dr = devres_alloc(devm_region_release, sizeof(struct region_devres), | |
1199 | GFP_KERNEL); | |
1200 | if (!dr) | |
1201 | return NULL; | |
1202 | ||
1203 | dr->parent = parent; | |
1204 | dr->start = start; | |
1205 | dr->n = n; | |
1206 | ||
e8de1481 | 1207 | res = __request_region(parent, start, n, name, 0); |
9ac7849e TH |
1208 | if (res) |
1209 | devres_add(dev, dr); | |
1210 | else | |
1211 | devres_free(dr); | |
1212 | ||
1213 | return res; | |
1214 | } | |
1215 | EXPORT_SYMBOL(__devm_request_region); | |
1216 | ||
1217 | void __devm_release_region(struct device *dev, struct resource *parent, | |
1218 | resource_size_t start, resource_size_t n) | |
1219 | { | |
1220 | struct region_devres match_data = { parent, start, n }; | |
1221 | ||
1222 | __release_region(parent, start, n); | |
1223 | WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match, | |
1224 | &match_data)); | |
1225 | } | |
1226 | EXPORT_SYMBOL(__devm_release_region); | |
1227 | ||
1da177e4 LT |
1228 | /* |
1229 | * Called from init/main.c to reserve IO ports. | |
1230 | */ | |
1231 | #define MAXRESERVE 4 | |
1232 | static int __init reserve_setup(char *str) | |
1233 | { | |
1234 | static int reserved; | |
1235 | static struct resource reserve[MAXRESERVE]; | |
1236 | ||
1237 | for (;;) { | |
8bc1ad7d | 1238 | unsigned int io_start, io_num; |
1da177e4 LT |
1239 | int x = reserved; |
1240 | ||
1241 | if (get_option (&str, &io_start) != 2) | |
1242 | break; | |
1243 | if (get_option (&str, &io_num) == 0) | |
1244 | break; | |
1245 | if (x < MAXRESERVE) { | |
1246 | struct resource *res = reserve + x; | |
1247 | res->name = "reserved"; | |
1248 | res->start = io_start; | |
1249 | res->end = io_start + io_num - 1; | |
1250 | res->flags = IORESOURCE_BUSY; | |
1251 | res->child = NULL; | |
1252 | if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0) | |
1253 | reserved = x+1; | |
1254 | } | |
1255 | } | |
1256 | return 1; | |
1257 | } | |
1258 | ||
1259 | __setup("reserve=", reserve_setup); | |
379daf62 SS |
1260 | |
1261 | /* | |
1262 | * Check if the requested addr and size spans more than any slot in the | |
1263 | * iomem resource tree. | |
1264 | */ | |
1265 | int iomem_map_sanity_check(resource_size_t addr, unsigned long size) | |
1266 | { | |
1267 | struct resource *p = &iomem_resource; | |
1268 | int err = 0; | |
1269 | loff_t l; | |
1270 | ||
1271 | read_lock(&resource_lock); | |
1272 | for (p = p->child; p ; p = r_next(NULL, p, &l)) { | |
1273 | /* | |
1274 | * We can probably skip the resources without | |
1275 | * IORESOURCE_IO attribute? | |
1276 | */ | |
1277 | if (p->start >= addr + size) | |
1278 | continue; | |
1279 | if (p->end < addr) | |
1280 | continue; | |
d68612b2 SS |
1281 | if (PFN_DOWN(p->start) <= PFN_DOWN(addr) && |
1282 | PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1)) | |
379daf62 | 1283 | continue; |
3ac52669 AV |
1284 | /* |
1285 | * if a resource is "BUSY", it's not a hardware resource | |
1286 | * but a driver mapping of such a resource; we don't want | |
1287 | * to warn for those; some drivers legitimately map only | |
1288 | * partial hardware resources. (example: vesafb) | |
1289 | */ | |
1290 | if (p->flags & IORESOURCE_BUSY) | |
1291 | continue; | |
1292 | ||
e4c72966 | 1293 | printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n", |
13eb8375 IM |
1294 | (unsigned long long)addr, |
1295 | (unsigned long long)(addr + size - 1), | |
e4c72966 | 1296 | p->name, p); |
379daf62 SS |
1297 | err = -1; |
1298 | break; | |
1299 | } | |
1300 | read_unlock(&resource_lock); | |
1301 | ||
1302 | return err; | |
1303 | } | |
e8de1481 AV |
1304 | |
1305 | #ifdef CONFIG_STRICT_DEVMEM | |
1306 | static int strict_iomem_checks = 1; | |
1307 | #else | |
1308 | static int strict_iomem_checks; | |
1309 | #endif | |
1310 | ||
1311 | /* | |
1312 | * check if an address is reserved in the iomem resource tree | |
1313 | * returns 1 if reserved, 0 if not reserved. | |
1314 | */ | |
1315 | int iomem_is_exclusive(u64 addr) | |
1316 | { | |
1317 | struct resource *p = &iomem_resource; | |
1318 | int err = 0; | |
1319 | loff_t l; | |
1320 | int size = PAGE_SIZE; | |
1321 | ||
1322 | if (!strict_iomem_checks) | |
1323 | return 0; | |
1324 | ||
1325 | addr = addr & PAGE_MASK; | |
1326 | ||
1327 | read_lock(&resource_lock); | |
1328 | for (p = p->child; p ; p = r_next(NULL, p, &l)) { | |
1329 | /* | |
1330 | * We can probably skip the resources without | |
1331 | * IORESOURCE_IO attribute? | |
1332 | */ | |
1333 | if (p->start >= addr + size) | |
1334 | break; | |
1335 | if (p->end < addr) | |
1336 | continue; | |
1337 | if (p->flags & IORESOURCE_BUSY && | |
1338 | p->flags & IORESOURCE_EXCLUSIVE) { | |
1339 | err = 1; | |
1340 | break; | |
1341 | } | |
1342 | } | |
1343 | read_unlock(&resource_lock); | |
1344 | ||
1345 | return err; | |
1346 | } | |
1347 | ||
1348 | static int __init strict_iomem(char *str) | |
1349 | { | |
1350 | if (strstr(str, "relaxed")) | |
1351 | strict_iomem_checks = 0; | |
1352 | if (strstr(str, "strict")) | |
1353 | strict_iomem_checks = 1; | |
1354 | return 1; | |
1355 | } | |
1356 | ||
1357 | __setup("iomem=", strict_iomem); |