]>
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> |
90e97820 | 25 | #include <linux/resource_ext.h> |
1da177e4 LT |
26 | #include <asm/io.h> |
27 | ||
28 | ||
29 | struct resource ioport_resource = { | |
30 | .name = "PCI IO", | |
6550e07f | 31 | .start = 0, |
1da177e4 LT |
32 | .end = IO_SPACE_LIMIT, |
33 | .flags = IORESOURCE_IO, | |
34 | }; | |
1da177e4 LT |
35 | EXPORT_SYMBOL(ioport_resource); |
36 | ||
37 | struct resource iomem_resource = { | |
38 | .name = "PCI mem", | |
6550e07f GKH |
39 | .start = 0, |
40 | .end = -1, | |
1da177e4 LT |
41 | .flags = IORESOURCE_MEM, |
42 | }; | |
1da177e4 LT |
43 | EXPORT_SYMBOL(iomem_resource); |
44 | ||
23c570a6 RP |
45 | /* constraints to be met while allocating resources */ |
46 | struct resource_constraint { | |
47 | resource_size_t min, max, align; | |
48 | resource_size_t (*alignf)(void *, const struct resource *, | |
49 | resource_size_t, resource_size_t); | |
50 | void *alignf_data; | |
51 | }; | |
52 | ||
1da177e4 LT |
53 | static DEFINE_RWLOCK(resource_lock); |
54 | ||
ebff7d8f YI |
55 | /* |
56 | * For memory hotplug, there is no way to free resource entries allocated | |
57 | * by boot mem after the system is up. So for reusing the resource entry | |
58 | * we need to remember the resource. | |
59 | */ | |
60 | static struct resource *bootmem_resource_free; | |
61 | static DEFINE_SPINLOCK(bootmem_resource_lock); | |
62 | ||
8c86e70a | 63 | static struct resource *next_resource(struct resource *p, bool sibling_only) |
1da177e4 | 64 | { |
8c86e70a VG |
65 | /* Caller wants to traverse through siblings only */ |
66 | if (sibling_only) | |
67 | return p->sibling; | |
68 | ||
1da177e4 LT |
69 | if (p->child) |
70 | return p->child; | |
71 | while (!p->sibling && p->parent) | |
72 | p = p->parent; | |
73 | return p->sibling; | |
74 | } | |
75 | ||
8c86e70a VG |
76 | static void *r_next(struct seq_file *m, void *v, loff_t *pos) |
77 | { | |
78 | struct resource *p = v; | |
79 | (*pos)++; | |
80 | return (void *)next_resource(p, false); | |
81 | } | |
82 | ||
13eb8375 IM |
83 | #ifdef CONFIG_PROC_FS |
84 | ||
85 | enum { MAX_IORES_LEVEL = 5 }; | |
86 | ||
1da177e4 LT |
87 | static void *r_start(struct seq_file *m, loff_t *pos) |
88 | __acquires(resource_lock) | |
89 | { | |
4e292a96 | 90 | struct resource *p = PDE_DATA(file_inode(m->file)); |
1da177e4 LT |
91 | loff_t l = 0; |
92 | read_lock(&resource_lock); | |
93 | for (p = p->child; p && l < *pos; p = r_next(m, p, &l)) | |
94 | ; | |
95 | return p; | |
96 | } | |
97 | ||
98 | static void r_stop(struct seq_file *m, void *v) | |
99 | __releases(resource_lock) | |
100 | { | |
101 | read_unlock(&resource_lock); | |
102 | } | |
103 | ||
104 | static int r_show(struct seq_file *m, void *v) | |
105 | { | |
4e292a96 | 106 | struct resource *root = PDE_DATA(file_inode(m->file)); |
1da177e4 | 107 | struct resource *r = v, *p; |
51d7b120 | 108 | unsigned long long start, end; |
1da177e4 LT |
109 | int width = root->end < 0x10000 ? 4 : 8; |
110 | int depth; | |
111 | ||
112 | for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent) | |
113 | if (p->parent == root) | |
114 | break; | |
51d7b120 LT |
115 | |
116 | if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) { | |
117 | start = r->start; | |
118 | end = r->end; | |
119 | } else { | |
120 | start = end = 0; | |
121 | } | |
122 | ||
685143ac | 123 | seq_printf(m, "%*s%0*llx-%0*llx : %s\n", |
1da177e4 | 124 | depth * 2, "", |
51d7b120 LT |
125 | width, start, |
126 | width, end, | |
1da177e4 LT |
127 | r->name ? r->name : "<BAD>"); |
128 | return 0; | |
129 | } | |
130 | ||
15ad7cdc | 131 | static const struct seq_operations resource_op = { |
1da177e4 LT |
132 | .start = r_start, |
133 | .next = r_next, | |
134 | .stop = r_stop, | |
135 | .show = r_show, | |
136 | }; | |
137 | ||
1da177e4 LT |
138 | static int __init ioresources_init(void) |
139 | { | |
4e292a96 CH |
140 | proc_create_seq_data("ioports", 0, NULL, &resource_op, |
141 | &ioport_resource); | |
142 | proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource); | |
1da177e4 LT |
143 | return 0; |
144 | } | |
145 | __initcall(ioresources_init); | |
146 | ||
147 | #endif /* CONFIG_PROC_FS */ | |
148 | ||
ebff7d8f YI |
149 | static void free_resource(struct resource *res) |
150 | { | |
151 | if (!res) | |
152 | return; | |
153 | ||
154 | if (!PageSlab(virt_to_head_page(res))) { | |
155 | spin_lock(&bootmem_resource_lock); | |
156 | res->sibling = bootmem_resource_free; | |
157 | bootmem_resource_free = res; | |
158 | spin_unlock(&bootmem_resource_lock); | |
159 | } else { | |
160 | kfree(res); | |
161 | } | |
162 | } | |
163 | ||
164 | static struct resource *alloc_resource(gfp_t flags) | |
165 | { | |
166 | struct resource *res = NULL; | |
167 | ||
168 | spin_lock(&bootmem_resource_lock); | |
169 | if (bootmem_resource_free) { | |
170 | res = bootmem_resource_free; | |
171 | bootmem_resource_free = res->sibling; | |
172 | } | |
173 | spin_unlock(&bootmem_resource_lock); | |
174 | ||
175 | if (res) | |
176 | memset(res, 0, sizeof(struct resource)); | |
177 | else | |
178 | res = kzalloc(sizeof(struct resource), flags); | |
179 | ||
180 | return res; | |
181 | } | |
182 | ||
1da177e4 LT |
183 | /* Return the conflict entry if you can't request it */ |
184 | static struct resource * __request_resource(struct resource *root, struct resource *new) | |
185 | { | |
d75fc8bb GKH |
186 | resource_size_t start = new->start; |
187 | resource_size_t end = new->end; | |
1da177e4 LT |
188 | struct resource *tmp, **p; |
189 | ||
190 | if (end < start) | |
191 | return root; | |
192 | if (start < root->start) | |
193 | return root; | |
194 | if (end > root->end) | |
195 | return root; | |
196 | p = &root->child; | |
197 | for (;;) { | |
198 | tmp = *p; | |
199 | if (!tmp || tmp->start > end) { | |
200 | new->sibling = tmp; | |
201 | *p = new; | |
202 | new->parent = root; | |
203 | return NULL; | |
204 | } | |
205 | p = &tmp->sibling; | |
206 | if (tmp->end < start) | |
207 | continue; | |
208 | return tmp; | |
209 | } | |
210 | } | |
211 | ||
ff3cc952 | 212 | static int __release_resource(struct resource *old, bool release_child) |
1da177e4 | 213 | { |
ff3cc952 | 214 | struct resource *tmp, **p, *chd; |
1da177e4 LT |
215 | |
216 | p = &old->parent->child; | |
217 | for (;;) { | |
218 | tmp = *p; | |
219 | if (!tmp) | |
220 | break; | |
221 | if (tmp == old) { | |
ff3cc952 TK |
222 | if (release_child || !(tmp->child)) { |
223 | *p = tmp->sibling; | |
224 | } else { | |
225 | for (chd = tmp->child;; chd = chd->sibling) { | |
226 | chd->parent = tmp->parent; | |
227 | if (!(chd->sibling)) | |
228 | break; | |
229 | } | |
230 | *p = tmp->child; | |
231 | chd->sibling = tmp->sibling; | |
232 | } | |
1da177e4 LT |
233 | old->parent = NULL; |
234 | return 0; | |
235 | } | |
236 | p = &tmp->sibling; | |
237 | } | |
238 | return -EINVAL; | |
239 | } | |
240 | ||
5eeec0ec YL |
241 | static void __release_child_resources(struct resource *r) |
242 | { | |
243 | struct resource *tmp, *p; | |
244 | resource_size_t size; | |
245 | ||
246 | p = r->child; | |
247 | r->child = NULL; | |
248 | while (p) { | |
249 | tmp = p; | |
250 | p = p->sibling; | |
251 | ||
252 | tmp->parent = NULL; | |
253 | tmp->sibling = NULL; | |
254 | __release_child_resources(tmp); | |
255 | ||
256 | printk(KERN_DEBUG "release child resource %pR\n", tmp); | |
257 | /* need to restore size, and keep flags */ | |
258 | size = resource_size(tmp); | |
259 | tmp->start = 0; | |
260 | tmp->end = size - 1; | |
261 | } | |
262 | } | |
263 | ||
264 | void release_child_resources(struct resource *r) | |
265 | { | |
266 | write_lock(&resource_lock); | |
267 | __release_child_resources(r); | |
268 | write_unlock(&resource_lock); | |
269 | } | |
270 | ||
e1ca66d1 | 271 | /** |
66f1207b | 272 | * request_resource_conflict - request and reserve an I/O or memory resource |
e1ca66d1 RD |
273 | * @root: root resource descriptor |
274 | * @new: resource descriptor desired by caller | |
275 | * | |
66f1207b | 276 | * Returns 0 for success, conflict resource on error. |
e1ca66d1 | 277 | */ |
66f1207b | 278 | struct resource *request_resource_conflict(struct resource *root, struct resource *new) |
1da177e4 LT |
279 | { |
280 | struct resource *conflict; | |
281 | ||
282 | write_lock(&resource_lock); | |
283 | conflict = __request_resource(root, new); | |
284 | write_unlock(&resource_lock); | |
66f1207b BH |
285 | return conflict; |
286 | } | |
287 | ||
288 | /** | |
289 | * request_resource - request and reserve an I/O or memory resource | |
290 | * @root: root resource descriptor | |
291 | * @new: resource descriptor desired by caller | |
292 | * | |
293 | * Returns 0 for success, negative error code on error. | |
294 | */ | |
295 | int request_resource(struct resource *root, struct resource *new) | |
296 | { | |
297 | struct resource *conflict; | |
298 | ||
299 | conflict = request_resource_conflict(root, new); | |
1da177e4 LT |
300 | return conflict ? -EBUSY : 0; |
301 | } | |
302 | ||
303 | EXPORT_SYMBOL(request_resource); | |
304 | ||
e1ca66d1 RD |
305 | /** |
306 | * release_resource - release a previously reserved resource | |
307 | * @old: resource pointer | |
308 | */ | |
1da177e4 LT |
309 | int release_resource(struct resource *old) |
310 | { | |
311 | int retval; | |
312 | ||
313 | write_lock(&resource_lock); | |
ff3cc952 | 314 | retval = __release_resource(old, true); |
1da177e4 LT |
315 | write_unlock(&resource_lock); |
316 | return retval; | |
317 | } | |
318 | ||
319 | EXPORT_SYMBOL(release_resource); | |
320 | ||
f26621e6 BP |
321 | /** |
322 | * Finds the lowest iomem resource that covers part of [@start..@end]. The | |
323 | * caller must specify @start, @end, @flags, and @desc (which may be | |
010a93bf BH |
324 | * IORES_DESC_NONE). |
325 | * | |
f26621e6 BP |
326 | * If a resource is found, returns 0 and @*res is overwritten with the part |
327 | * of the resource that's within [@start..@end]; if none is found, returns | |
328 | * -1 or -EINVAL for other invalid parameters. | |
010a93bf BH |
329 | * |
330 | * This function walks the whole tree and not just first level children | |
b69c2e20 | 331 | * unless @first_lvl is true. |
f26621e6 BP |
332 | * |
333 | * @start: start address of the resource searched for | |
334 | * @end: end address of same resource | |
335 | * @flags: flags which the resource must have | |
336 | * @desc: descriptor the resource must have | |
337 | * @first_lvl: walk only the first level children, if set | |
338 | * @res: return ptr, if resource found | |
2842f114 | 339 | */ |
010a93bf BH |
340 | static int find_next_iomem_res(resource_size_t start, resource_size_t end, |
341 | unsigned long flags, unsigned long desc, | |
b69c2e20 | 342 | bool first_lvl, struct resource *res) |
2842f114 | 343 | { |
2842f114 KH |
344 | struct resource *p; |
345 | ||
b69c2e20 BP |
346 | if (!res) |
347 | return -EINVAL; | |
2842f114 | 348 | |
b69c2e20 BP |
349 | if (start >= end) |
350 | return -EINVAL; | |
8c86e70a | 351 | |
800df627 VG |
352 | read_lock(&resource_lock); |
353 | ||
b69c2e20 | 354 | for (p = iomem_resource.child; p; p = next_resource(p, first_lvl)) { |
010a93bf | 355 | if ((p->flags & flags) != flags) |
2842f114 | 356 | continue; |
3f33647c TK |
357 | if ((desc != IORES_DESC_NONE) && (desc != p->desc)) |
358 | continue; | |
2842f114 KH |
359 | if (p->start > end) { |
360 | p = NULL; | |
361 | break; | |
362 | } | |
a98959fd | 363 | if ((p->end >= start) && (p->start <= end)) |
2842f114 KH |
364 | break; |
365 | } | |
8c86e70a | 366 | |
2842f114 KH |
367 | read_unlock(&resource_lock); |
368 | if (!p) | |
369 | return -1; | |
010a93bf | 370 | |
2842f114 | 371 | /* copy data */ |
010a93bf BH |
372 | res->start = max(start, p->start); |
373 | res->end = min(end, p->end); | |
0e4c12b4 TL |
374 | res->flags = p->flags; |
375 | res->desc = p->desc; | |
2842f114 KH |
376 | return 0; |
377 | } | |
908eedc6 | 378 | |
010a93bf BH |
379 | static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end, |
380 | unsigned long flags, unsigned long desc, | |
b69c2e20 | 381 | bool first_lvl, void *arg, |
1d2e733b | 382 | int (*func)(struct resource *, void *)) |
4ac2aed8 | 383 | { |
010a93bf | 384 | struct resource res; |
4ac2aed8 TL |
385 | int ret = -1; |
386 | ||
010a93bf | 387 | while (start < end && |
b69c2e20 | 388 | !find_next_iomem_res(start, end, flags, desc, first_lvl, &res)) { |
010a93bf | 389 | ret = (*func)(&res, arg); |
4ac2aed8 TL |
390 | if (ret) |
391 | break; | |
392 | ||
010a93bf | 393 | start = res.end + 1; |
4ac2aed8 TL |
394 | } |
395 | ||
396 | return ret; | |
397 | } | |
398 | ||
b69c2e20 | 399 | /** |
f26621e6 | 400 | * Walks through iomem resources and calls func() with matching resource |
8c86e70a VG |
401 | * ranges. This walks through whole tree and not just first level children. |
402 | * All the memory ranges which overlap start,end and also match flags and | |
3f33647c TK |
403 | * desc are valid candidates. |
404 | * | |
405 | * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check. | |
406 | * @flags: I/O resource flags | |
407 | * @start: start addr | |
408 | * @end: end addr | |
f75d6515 RD |
409 | * @arg: function argument for the callback @func |
410 | * @func: callback function that is called for each qualifying resource area | |
3f33647c TK |
411 | * |
412 | * NOTE: For a new descriptor search, define a new IORES_DESC in | |
413 | * <linux/ioport.h> and set it in 'desc' of a target resource entry. | |
414 | */ | |
415 | int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, | |
1d2e733b | 416 | u64 end, void *arg, int (*func)(struct resource *, void *)) |
3f33647c | 417 | { |
010a93bf | 418 | return __walk_iomem_res_desc(start, end, flags, desc, false, arg, func); |
3f33647c | 419 | } |
d76401ad | 420 | EXPORT_SYMBOL_GPL(walk_iomem_res_desc); |
3f33647c | 421 | |
8c86e70a | 422 | /* |
bd7e6cb3 TK |
423 | * This function calls the @func callback against all memory ranges of type |
424 | * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY. | |
425 | * Now, this function is only for System RAM, it deals with full ranges and | |
426 | * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate | |
427 | * ranges. | |
8c86e70a VG |
428 | */ |
429 | int walk_system_ram_res(u64 start, u64 end, void *arg, | |
b69c2e20 | 430 | int (*func)(struct resource *, void *)) |
8c86e70a | 431 | { |
010a93bf | 432 | unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; |
8c86e70a | 433 | |
010a93bf | 434 | return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, true, |
0e4c12b4 TL |
435 | arg, func); |
436 | } | |
437 | ||
438 | /* | |
439 | * This function calls the @func callback against all memory ranges, which | |
440 | * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY. | |
441 | */ | |
442 | int walk_mem_res(u64 start, u64 end, void *arg, | |
443 | int (*func)(struct resource *, void *)) | |
444 | { | |
010a93bf | 445 | unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
0e4c12b4 | 446 | |
010a93bf | 447 | return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, true, |
4ac2aed8 | 448 | arg, func); |
8c86e70a VG |
449 | } |
450 | ||
908eedc6 | 451 | /* |
bd7e6cb3 TK |
452 | * This function calls the @func callback against all memory ranges of type |
453 | * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY. | |
454 | * It is to be used only for System RAM. | |
908eedc6 KH |
455 | */ |
456 | int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages, | |
b69c2e20 | 457 | void *arg, int (*func)(unsigned long, unsigned long, void *)) |
75884fb1 | 458 | { |
010a93bf BH |
459 | resource_size_t start, end; |
460 | unsigned long flags; | |
75884fb1 | 461 | struct resource res; |
37b99dd5 | 462 | unsigned long pfn, end_pfn; |
75884fb1 | 463 | int ret = -1; |
908eedc6 | 464 | |
010a93bf BH |
465 | start = (u64) start_pfn << PAGE_SHIFT; |
466 | end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1; | |
467 | flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; | |
468 | while (start < end && | |
469 | !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, | |
470 | true, &res)) { | |
37b99dd5 WF |
471 | pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT; |
472 | end_pfn = (res.end + 1) >> PAGE_SHIFT; | |
473 | if (end_pfn > pfn) | |
f4149660 | 474 | ret = (*func)(pfn, end_pfn - pfn, arg); |
75884fb1 KH |
475 | if (ret) |
476 | break; | |
010a93bf | 477 | start = res.end + 1; |
75884fb1 KH |
478 | } |
479 | return ret; | |
480 | } | |
481 | ||
61ef2489 WF |
482 | static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg) |
483 | { | |
484 | return 1; | |
485 | } | |
4ac2aed8 | 486 | |
61ef2489 WF |
487 | /* |
488 | * This generic page_is_ram() returns true if specified address is | |
bd7e6cb3 | 489 | * registered as System RAM in iomem_resource list. |
61ef2489 | 490 | */ |
e5273007 | 491 | int __weak page_is_ram(unsigned long pfn) |
61ef2489 WF |
492 | { |
493 | return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1; | |
494 | } | |
c5a13032 | 495 | EXPORT_SYMBOL_GPL(page_is_ram); |
61ef2489 | 496 | |
124fe20d DW |
497 | /** |
498 | * region_intersects() - determine intersection of region with known resources | |
499 | * @start: region start address | |
500 | * @size: size of region | |
1c29f25b TK |
501 | * @flags: flags of resource (in iomem_resource) |
502 | * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE | |
67cf13ce | 503 | * |
124fe20d | 504 | * Check if the specified region partially overlaps or fully eclipses a |
1c29f25b TK |
505 | * resource identified by @flags and @desc (optional with IORES_DESC_NONE). |
506 | * Return REGION_DISJOINT if the region does not overlap @flags/@desc, | |
507 | * return REGION_MIXED if the region overlaps @flags/@desc and another | |
508 | * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc | |
509 | * and no other defined resource. Note that REGION_INTERSECTS is also | |
510 | * returned in the case when the specified region overlaps RAM and undefined | |
511 | * memory holes. | |
124fe20d DW |
512 | * |
513 | * region_intersect() is used by memory remapping functions to ensure | |
514 | * the user is not remapping RAM and is a vast speed up over walking | |
515 | * through the resource table page by page. | |
67cf13ce | 516 | */ |
1c29f25b TK |
517 | int region_intersects(resource_size_t start, size_t size, unsigned long flags, |
518 | unsigned long desc) | |
67cf13ce | 519 | { |
124fe20d DW |
520 | resource_size_t end = start + size - 1; |
521 | int type = 0; int other = 0; | |
522 | struct resource *p; | |
67cf13ce MT |
523 | |
524 | read_lock(&resource_lock); | |
525 | for (p = iomem_resource.child; p ; p = p->sibling) { | |
1c29f25b TK |
526 | bool is_type = (((p->flags & flags) == flags) && |
527 | ((desc == IORES_DESC_NONE) || | |
528 | (desc == p->desc))); | |
124fe20d DW |
529 | |
530 | if (start >= p->start && start <= p->end) | |
531 | is_type ? type++ : other++; | |
532 | if (end >= p->start && end <= p->end) | |
533 | is_type ? type++ : other++; | |
534 | if (p->start >= start && p->end <= end) | |
535 | is_type ? type++ : other++; | |
67cf13ce MT |
536 | } |
537 | read_unlock(&resource_lock); | |
124fe20d DW |
538 | |
539 | if (other == 0) | |
540 | return type ? REGION_INTERSECTS : REGION_DISJOINT; | |
541 | ||
542 | if (type) | |
543 | return REGION_MIXED; | |
544 | ||
545 | return REGION_DISJOINT; | |
67cf13ce | 546 | } |
1c29f25b | 547 | EXPORT_SYMBOL_GPL(region_intersects); |
67cf13ce | 548 | |
fcb11918 BH |
549 | void __weak arch_remove_reservations(struct resource *avail) |
550 | { | |
551 | } | |
552 | ||
a9cea017 BH |
553 | static resource_size_t simple_align_resource(void *data, |
554 | const struct resource *avail, | |
555 | resource_size_t size, | |
556 | resource_size_t align) | |
557 | { | |
558 | return avail->start; | |
559 | } | |
560 | ||
5d6b1fa3 BH |
561 | static void resource_clip(struct resource *res, resource_size_t min, |
562 | resource_size_t max) | |
563 | { | |
564 | if (res->start < min) | |
565 | res->start = min; | |
566 | if (res->end > max) | |
567 | res->end = max; | |
568 | } | |
569 | ||
1da177e4 | 570 | /* |
23c570a6 RP |
571 | * Find empty slot in the resource tree with the given range and |
572 | * alignment constraints | |
1da177e4 | 573 | */ |
23c570a6 RP |
574 | static int __find_resource(struct resource *root, struct resource *old, |
575 | struct resource *new, | |
576 | resource_size_t size, | |
577 | struct resource_constraint *constraint) | |
1da177e4 LT |
578 | { |
579 | struct resource *this = root->child; | |
a1862e31 | 580 | struct resource tmp = *new, avail, alloc; |
1da177e4 | 581 | |
0e2c8b8f | 582 | tmp.start = root->start; |
1da177e4 | 583 | /* |
c0f5ac54 BH |
584 | * Skip past an allocated resource that starts at 0, since the assignment |
585 | * of this->start - 1 to tmp->end below would cause an underflow. | |
1da177e4 | 586 | */ |
23c570a6 RP |
587 | if (this && this->start == root->start) { |
588 | tmp.start = (this == old) ? old->start : this->end + 1; | |
1da177e4 LT |
589 | this = this->sibling; |
590 | } | |
c0f5ac54 | 591 | for(;;) { |
1da177e4 | 592 | if (this) |
23c570a6 | 593 | tmp.end = (this == old) ? this->end : this->start - 1; |
1da177e4 | 594 | else |
0e2c8b8f | 595 | tmp.end = root->end; |
5d6b1fa3 | 596 | |
47ea91b4 RP |
597 | if (tmp.end < tmp.start) |
598 | goto next; | |
599 | ||
23c570a6 | 600 | resource_clip(&tmp, constraint->min, constraint->max); |
fcb11918 | 601 | arch_remove_reservations(&tmp); |
a9cea017 | 602 | |
a1862e31 | 603 | /* Check for overflow after ALIGN() */ |
23c570a6 | 604 | avail.start = ALIGN(tmp.start, constraint->align); |
a1862e31 | 605 | avail.end = tmp.end; |
5edb93b8 | 606 | avail.flags = new->flags & ~IORESOURCE_UNSET; |
a1862e31 | 607 | if (avail.start >= tmp.start) { |
5edb93b8 | 608 | alloc.flags = avail.flags; |
23c570a6 RP |
609 | alloc.start = constraint->alignf(constraint->alignf_data, &avail, |
610 | size, constraint->align); | |
a1862e31 | 611 | alloc.end = alloc.start + size - 1; |
60bb83b8 TI |
612 | if (alloc.start <= alloc.end && |
613 | resource_contains(&avail, &alloc)) { | |
a1862e31 BH |
614 | new->start = alloc.start; |
615 | new->end = alloc.end; | |
616 | return 0; | |
617 | } | |
1da177e4 | 618 | } |
47ea91b4 RP |
619 | |
620 | next: if (!this || this->end == root->end) | |
1da177e4 | 621 | break; |
47ea91b4 | 622 | |
23c570a6 RP |
623 | if (this != old) |
624 | tmp.start = this->end + 1; | |
1da177e4 LT |
625 | this = this->sibling; |
626 | } | |
627 | return -EBUSY; | |
628 | } | |
629 | ||
23c570a6 RP |
630 | /* |
631 | * Find empty slot in the resource tree given range and alignment. | |
632 | */ | |
633 | static int find_resource(struct resource *root, struct resource *new, | |
634 | resource_size_t size, | |
635 | struct resource_constraint *constraint) | |
636 | { | |
637 | return __find_resource(root, NULL, new, size, constraint); | |
638 | } | |
639 | ||
e1ca66d1 | 640 | /** |
23c570a6 RP |
641 | * reallocate_resource - allocate a slot in the resource tree given range & alignment. |
642 | * The resource will be relocated if the new size cannot be reallocated in the | |
643 | * current location. | |
644 | * | |
645 | * @root: root resource descriptor | |
646 | * @old: resource descriptor desired by caller | |
647 | * @newsize: new size of the resource descriptor | |
648 | * @constraint: the size and alignment constraints to be met. | |
649 | */ | |
28ab49ff | 650 | static int reallocate_resource(struct resource *root, struct resource *old, |
b69c2e20 BP |
651 | resource_size_t newsize, |
652 | struct resource_constraint *constraint) | |
23c570a6 RP |
653 | { |
654 | int err=0; | |
655 | struct resource new = *old; | |
656 | struct resource *conflict; | |
657 | ||
658 | write_lock(&resource_lock); | |
659 | ||
660 | if ((err = __find_resource(root, old, &new, newsize, constraint))) | |
661 | goto out; | |
662 | ||
663 | if (resource_contains(&new, old)) { | |
664 | old->start = new.start; | |
665 | old->end = new.end; | |
666 | goto out; | |
667 | } | |
668 | ||
669 | if (old->child) { | |
670 | err = -EBUSY; | |
671 | goto out; | |
672 | } | |
673 | ||
674 | if (resource_contains(old, &new)) { | |
675 | old->start = new.start; | |
676 | old->end = new.end; | |
677 | } else { | |
ff3cc952 | 678 | __release_resource(old, true); |
23c570a6 RP |
679 | *old = new; |
680 | conflict = __request_resource(root, old); | |
681 | BUG_ON(conflict); | |
682 | } | |
683 | out: | |
684 | write_unlock(&resource_lock); | |
685 | return err; | |
686 | } | |
687 | ||
688 | ||
689 | /** | |
690 | * allocate_resource - allocate empty slot in the resource tree given range & alignment. | |
691 | * The resource will be reallocated with a new size if it was already allocated | |
e1ca66d1 RD |
692 | * @root: root resource descriptor |
693 | * @new: resource descriptor desired by caller | |
694 | * @size: requested resource region size | |
ee5e5683 WY |
695 | * @min: minimum boundary to allocate |
696 | * @max: maximum boundary to allocate | |
e1ca66d1 RD |
697 | * @align: alignment requested, in bytes |
698 | * @alignf: alignment function, optional, called if not NULL | |
699 | * @alignf_data: arbitrary data to pass to the @alignf function | |
1da177e4 LT |
700 | */ |
701 | int allocate_resource(struct resource *root, struct resource *new, | |
d75fc8bb GKH |
702 | resource_size_t size, resource_size_t min, |
703 | resource_size_t max, resource_size_t align, | |
b26b2d49 | 704 | resource_size_t (*alignf)(void *, |
3b7a17fc | 705 | const struct resource *, |
b26b2d49 DB |
706 | resource_size_t, |
707 | resource_size_t), | |
1da177e4 LT |
708 | void *alignf_data) |
709 | { | |
710 | int err; | |
23c570a6 | 711 | struct resource_constraint constraint; |
1da177e4 | 712 | |
a9cea017 BH |
713 | if (!alignf) |
714 | alignf = simple_align_resource; | |
715 | ||
23c570a6 RP |
716 | constraint.min = min; |
717 | constraint.max = max; | |
718 | constraint.align = align; | |
719 | constraint.alignf = alignf; | |
720 | constraint.alignf_data = alignf_data; | |
721 | ||
722 | if ( new->parent ) { | |
723 | /* resource is already allocated, try reallocating with | |
724 | the new constraints */ | |
725 | return reallocate_resource(root, new, size, &constraint); | |
726 | } | |
727 | ||
1da177e4 | 728 | write_lock(&resource_lock); |
23c570a6 | 729 | err = find_resource(root, new, size, &constraint); |
1da177e4 LT |
730 | if (err >= 0 && __request_resource(root, new)) |
731 | err = -EBUSY; | |
732 | write_unlock(&resource_lock); | |
733 | return err; | |
734 | } | |
735 | ||
736 | EXPORT_SYMBOL(allocate_resource); | |
737 | ||
1c388919 GU |
738 | /** |
739 | * lookup_resource - find an existing resource by a resource start address | |
740 | * @root: root resource descriptor | |
741 | * @start: resource start address | |
742 | * | |
743 | * Returns a pointer to the resource if found, NULL otherwise | |
744 | */ | |
745 | struct resource *lookup_resource(struct resource *root, resource_size_t start) | |
746 | { | |
747 | struct resource *res; | |
748 | ||
749 | read_lock(&resource_lock); | |
750 | for (res = root->child; res; res = res->sibling) { | |
751 | if (res->start == start) | |
752 | break; | |
753 | } | |
754 | read_unlock(&resource_lock); | |
755 | ||
756 | return res; | |
757 | } | |
758 | ||
bef69ea0 LT |
759 | /* |
760 | * Insert a resource into the resource tree. If successful, return NULL, | |
761 | * otherwise return the conflicting resource (compare to __request_resource()) | |
1da177e4 | 762 | */ |
bef69ea0 | 763 | static struct resource * __insert_resource(struct resource *parent, struct resource *new) |
1da177e4 | 764 | { |
1da177e4 LT |
765 | struct resource *first, *next; |
766 | ||
d33b6fba | 767 | for (;; parent = first) { |
d33b6fba MW |
768 | first = __request_resource(parent, new); |
769 | if (!first) | |
bef69ea0 | 770 | return first; |
d33b6fba | 771 | |
d33b6fba | 772 | if (first == parent) |
bef69ea0 | 773 | return first; |
5de1cb2d HS |
774 | if (WARN_ON(first == new)) /* duplicated insertion */ |
775 | return first; | |
d33b6fba MW |
776 | |
777 | if ((first->start > new->start) || (first->end < new->end)) | |
778 | break; | |
779 | if ((first->start == new->start) && (first->end == new->end)) | |
780 | break; | |
1da177e4 LT |
781 | } |
782 | ||
783 | for (next = first; ; next = next->sibling) { | |
784 | /* Partial overlap? Bad, and unfixable */ | |
785 | if (next->start < new->start || next->end > new->end) | |
bef69ea0 | 786 | return next; |
1da177e4 LT |
787 | if (!next->sibling) |
788 | break; | |
789 | if (next->sibling->start > new->end) | |
790 | break; | |
791 | } | |
792 | ||
1da177e4 LT |
793 | new->parent = parent; |
794 | new->sibling = next->sibling; | |
795 | new->child = first; | |
796 | ||
797 | next->sibling = NULL; | |
798 | for (next = first; next; next = next->sibling) | |
799 | next->parent = new; | |
800 | ||
801 | if (parent->child == first) { | |
802 | parent->child = new; | |
803 | } else { | |
804 | next = parent->child; | |
805 | while (next->sibling != first) | |
806 | next = next->sibling; | |
807 | next->sibling = new; | |
808 | } | |
bef69ea0 LT |
809 | return NULL; |
810 | } | |
1da177e4 | 811 | |
bef69ea0 | 812 | /** |
66f1207b | 813 | * insert_resource_conflict - Inserts resource in the resource tree |
bef69ea0 LT |
814 | * @parent: parent of the new resource |
815 | * @new: new resource to insert | |
816 | * | |
66f1207b | 817 | * Returns 0 on success, conflict resource if the resource can't be inserted. |
bef69ea0 | 818 | * |
66f1207b | 819 | * This function is equivalent to request_resource_conflict when no conflict |
bef69ea0 LT |
820 | * happens. If a conflict happens, and the conflicting resources |
821 | * entirely fit within the range of the new resource, then the new | |
822 | * resource is inserted and the conflicting resources become children of | |
823 | * the new resource. | |
ff3cc952 TK |
824 | * |
825 | * This function is intended for producers of resources, such as FW modules | |
826 | * and bus drivers. | |
bef69ea0 | 827 | */ |
66f1207b | 828 | struct resource *insert_resource_conflict(struct resource *parent, struct resource *new) |
bef69ea0 LT |
829 | { |
830 | struct resource *conflict; | |
831 | ||
832 | write_lock(&resource_lock); | |
833 | conflict = __insert_resource(parent, new); | |
834 | write_unlock(&resource_lock); | |
66f1207b BH |
835 | return conflict; |
836 | } | |
837 | ||
838 | /** | |
839 | * insert_resource - Inserts a resource in the resource tree | |
840 | * @parent: parent of the new resource | |
841 | * @new: new resource to insert | |
842 | * | |
843 | * Returns 0 on success, -EBUSY if the resource can't be inserted. | |
ff3cc952 TK |
844 | * |
845 | * This function is intended for producers of resources, such as FW modules | |
846 | * and bus drivers. | |
66f1207b BH |
847 | */ |
848 | int insert_resource(struct resource *parent, struct resource *new) | |
849 | { | |
850 | struct resource *conflict; | |
851 | ||
852 | conflict = insert_resource_conflict(parent, new); | |
bef69ea0 LT |
853 | return conflict ? -EBUSY : 0; |
854 | } | |
8095d0f2 | 855 | EXPORT_SYMBOL_GPL(insert_resource); |
bef69ea0 LT |
856 | |
857 | /** | |
858 | * insert_resource_expand_to_fit - Insert a resource into the resource tree | |
6781f4ae | 859 | * @root: root resource descriptor |
bef69ea0 LT |
860 | * @new: new resource to insert |
861 | * | |
862 | * Insert a resource into the resource tree, possibly expanding it in order | |
863 | * to make it encompass any conflicting resources. | |
864 | */ | |
865 | void insert_resource_expand_to_fit(struct resource *root, struct resource *new) | |
866 | { | |
867 | if (new->parent) | |
868 | return; | |
869 | ||
870 | write_lock(&resource_lock); | |
871 | for (;;) { | |
872 | struct resource *conflict; | |
873 | ||
874 | conflict = __insert_resource(root, new); | |
875 | if (!conflict) | |
876 | break; | |
877 | if (conflict == root) | |
878 | break; | |
879 | ||
880 | /* Ok, expand resource to cover the conflict, then try again .. */ | |
881 | if (conflict->start < new->start) | |
882 | new->start = conflict->start; | |
883 | if (conflict->end > new->end) | |
884 | new->end = conflict->end; | |
885 | ||
886 | printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name); | |
887 | } | |
1da177e4 | 888 | write_unlock(&resource_lock); |
1da177e4 LT |
889 | } |
890 | ||
ff3cc952 TK |
891 | /** |
892 | * remove_resource - Remove a resource in the resource tree | |
893 | * @old: resource to remove | |
894 | * | |
895 | * Returns 0 on success, -EINVAL if the resource is not valid. | |
896 | * | |
897 | * This function removes a resource previously inserted by insert_resource() | |
898 | * or insert_resource_conflict(), and moves the children (if any) up to | |
899 | * where they were before. insert_resource() and insert_resource_conflict() | |
900 | * insert a new resource, and move any conflicting resources down to the | |
901 | * children of the new resource. | |
902 | * | |
903 | * insert_resource(), insert_resource_conflict() and remove_resource() are | |
904 | * intended for producers of resources, such as FW modules and bus drivers. | |
905 | */ | |
906 | int remove_resource(struct resource *old) | |
907 | { | |
908 | int retval; | |
909 | ||
910 | write_lock(&resource_lock); | |
911 | retval = __release_resource(old, false); | |
912 | write_unlock(&resource_lock); | |
913 | return retval; | |
914 | } | |
8095d0f2 | 915 | EXPORT_SYMBOL_GPL(remove_resource); |
ff3cc952 | 916 | |
ae8e3a91 TK |
917 | static int __adjust_resource(struct resource *res, resource_size_t start, |
918 | resource_size_t size) | |
1da177e4 LT |
919 | { |
920 | struct resource *tmp, *parent = res->parent; | |
d75fc8bb | 921 | resource_size_t end = start + size - 1; |
1da177e4 LT |
922 | int result = -EBUSY; |
923 | ||
82ec90ea YL |
924 | if (!parent) |
925 | goto skip; | |
926 | ||
1da177e4 LT |
927 | if ((start < parent->start) || (end > parent->end)) |
928 | goto out; | |
929 | ||
1da177e4 LT |
930 | if (res->sibling && (res->sibling->start <= end)) |
931 | goto out; | |
932 | ||
933 | tmp = parent->child; | |
934 | if (tmp != res) { | |
935 | while (tmp->sibling != res) | |
936 | tmp = tmp->sibling; | |
937 | if (start <= tmp->end) | |
938 | goto out; | |
939 | } | |
940 | ||
82ec90ea YL |
941 | skip: |
942 | for (tmp = res->child; tmp; tmp = tmp->sibling) | |
943 | if ((tmp->start < start) || (tmp->end > end)) | |
944 | goto out; | |
945 | ||
1da177e4 LT |
946 | res->start = start; |
947 | res->end = end; | |
948 | result = 0; | |
949 | ||
950 | out: | |
ae8e3a91 TK |
951 | return result; |
952 | } | |
953 | ||
954 | /** | |
955 | * adjust_resource - modify a resource's start and size | |
956 | * @res: resource to modify | |
957 | * @start: new start value | |
958 | * @size: new size | |
959 | * | |
960 | * Given an existing resource, change its start and size to match the | |
961 | * arguments. Returns 0 on success, -EBUSY if it can't fit. | |
962 | * Existing children of the resource are assumed to be immutable. | |
963 | */ | |
964 | int adjust_resource(struct resource *res, resource_size_t start, | |
b69c2e20 | 965 | resource_size_t size) |
ae8e3a91 TK |
966 | { |
967 | int result; | |
968 | ||
969 | write_lock(&resource_lock); | |
970 | result = __adjust_resource(res, start, size); | |
1da177e4 LT |
971 | write_unlock(&resource_lock); |
972 | return result; | |
973 | } | |
24105748 | 974 | EXPORT_SYMBOL(adjust_resource); |
1da177e4 | 975 | |
b69c2e20 BP |
976 | static void __init |
977 | __reserve_region_with_split(struct resource *root, resource_size_t start, | |
978 | resource_size_t end, const char *name) | |
268364a0 YL |
979 | { |
980 | struct resource *parent = root; | |
981 | struct resource *conflict; | |
ebff7d8f | 982 | struct resource *res = alloc_resource(GFP_ATOMIC); |
4965f566 | 983 | struct resource *next_res = NULL; |
f37e2334 | 984 | int type = resource_type(root); |
268364a0 YL |
985 | |
986 | if (!res) | |
987 | return; | |
988 | ||
989 | res->name = name; | |
990 | res->start = start; | |
991 | res->end = end; | |
f37e2334 | 992 | res->flags = type | IORESOURCE_BUSY; |
43ee493b | 993 | res->desc = IORES_DESC_NONE; |
268364a0 | 994 | |
4965f566 | 995 | while (1) { |
268364a0 | 996 | |
4965f566 M |
997 | conflict = __request_resource(parent, res); |
998 | if (!conflict) { | |
999 | if (!next_res) | |
1000 | break; | |
1001 | res = next_res; | |
1002 | next_res = NULL; | |
1003 | continue; | |
1004 | } | |
268364a0 | 1005 | |
4965f566 M |
1006 | /* conflict covered whole area */ |
1007 | if (conflict->start <= res->start && | |
1008 | conflict->end >= res->end) { | |
ebff7d8f | 1009 | free_resource(res); |
4965f566 M |
1010 | WARN_ON(next_res); |
1011 | break; | |
1012 | } | |
1013 | ||
1014 | /* failed, split and try again */ | |
1015 | if (conflict->start > res->start) { | |
1016 | end = res->end; | |
1017 | res->end = conflict->start - 1; | |
1018 | if (conflict->end < end) { | |
ebff7d8f | 1019 | next_res = alloc_resource(GFP_ATOMIC); |
4965f566 | 1020 | if (!next_res) { |
ebff7d8f | 1021 | free_resource(res); |
4965f566 M |
1022 | break; |
1023 | } | |
1024 | next_res->name = name; | |
1025 | next_res->start = conflict->end + 1; | |
1026 | next_res->end = end; | |
f37e2334 | 1027 | next_res->flags = type | IORESOURCE_BUSY; |
43ee493b | 1028 | next_res->desc = IORES_DESC_NONE; |
4965f566 M |
1029 | } |
1030 | } else { | |
1031 | res->start = conflict->end + 1; | |
1032 | } | |
1033 | } | |
268364a0 YL |
1034 | |
1035 | } | |
1036 | ||
b69c2e20 BP |
1037 | void __init |
1038 | reserve_region_with_split(struct resource *root, resource_size_t start, | |
1039 | resource_size_t end, const char *name) | |
268364a0 | 1040 | { |
65fed8f6 OP |
1041 | int abort = 0; |
1042 | ||
268364a0 | 1043 | write_lock(&resource_lock); |
65fed8f6 OP |
1044 | if (root->start > start || root->end < end) { |
1045 | pr_err("requested range [0x%llx-0x%llx] not in root %pr\n", | |
1046 | (unsigned long long)start, (unsigned long long)end, | |
1047 | root); | |
1048 | if (start > root->end || end < root->start) | |
1049 | abort = 1; | |
1050 | else { | |
1051 | if (end > root->end) | |
1052 | end = root->end; | |
1053 | if (start < root->start) | |
1054 | start = root->start; | |
1055 | pr_err("fixing request to [0x%llx-0x%llx]\n", | |
1056 | (unsigned long long)start, | |
1057 | (unsigned long long)end); | |
1058 | } | |
1059 | dump_stack(); | |
1060 | } | |
1061 | if (!abort) | |
1062 | __reserve_region_with_split(root, start, end, name); | |
268364a0 YL |
1063 | write_unlock(&resource_lock); |
1064 | } | |
1065 | ||
88452565 IK |
1066 | /** |
1067 | * resource_alignment - calculate resource's alignment | |
1068 | * @res: resource pointer | |
1069 | * | |
1070 | * Returns alignment on success, 0 (invalid alignment) on failure. | |
1071 | */ | |
1072 | resource_size_t resource_alignment(struct resource *res) | |
1073 | { | |
1074 | switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) { | |
1075 | case IORESOURCE_SIZEALIGN: | |
1a4e564b | 1076 | return resource_size(res); |
88452565 IK |
1077 | case IORESOURCE_STARTALIGN: |
1078 | return res->start; | |
1079 | default: | |
1080 | return 0; | |
1081 | } | |
1082 | } | |
1083 | ||
1da177e4 LT |
1084 | /* |
1085 | * This is compatibility stuff for IO resources. | |
1086 | * | |
1087 | * Note how this, unlike the above, knows about | |
1088 | * the IO flag meanings (busy etc). | |
1089 | * | |
e1ca66d1 | 1090 | * request_region creates a new busy region. |
1da177e4 | 1091 | * |
e1ca66d1 RD |
1092 | * release_region releases a matching busy region. |
1093 | */ | |
1094 | ||
8b6d043b AC |
1095 | static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait); |
1096 | ||
e1ca66d1 RD |
1097 | /** |
1098 | * __request_region - create a new busy resource region | |
1099 | * @parent: parent resource descriptor | |
1100 | * @start: resource start address | |
1101 | * @n: resource region size | |
1102 | * @name: reserving caller's ID string | |
6ae301e8 | 1103 | * @flags: IO resource flags |
1da177e4 | 1104 | */ |
d75fc8bb GKH |
1105 | struct resource * __request_region(struct resource *parent, |
1106 | resource_size_t start, resource_size_t n, | |
e8de1481 | 1107 | const char *name, int flags) |
1da177e4 | 1108 | { |
8b6d043b | 1109 | DECLARE_WAITQUEUE(wait, current); |
ebff7d8f | 1110 | struct resource *res = alloc_resource(GFP_KERNEL); |
1da177e4 | 1111 | |
c26ec88e BH |
1112 | if (!res) |
1113 | return NULL; | |
1114 | ||
1115 | res->name = name; | |
1116 | res->start = start; | |
1117 | res->end = start + n - 1; | |
c26ec88e BH |
1118 | |
1119 | write_lock(&resource_lock); | |
1120 | ||
1121 | for (;;) { | |
1122 | struct resource *conflict; | |
1123 | ||
4e0d8f7e TK |
1124 | res->flags = resource_type(parent) | resource_ext_type(parent); |
1125 | res->flags |= IORESOURCE_BUSY | flags; | |
1126 | res->desc = parent->desc; | |
1127 | ||
c26ec88e BH |
1128 | conflict = __request_resource(parent, res); |
1129 | if (!conflict) | |
1da177e4 | 1130 | break; |
c26ec88e | 1131 | if (conflict != parent) { |
59ceeaaf SG |
1132 | if (!(conflict->flags & IORESOURCE_BUSY)) { |
1133 | parent = conflict; | |
c26ec88e | 1134 | continue; |
59ceeaaf | 1135 | } |
1da177e4 | 1136 | } |
8b6d043b AC |
1137 | if (conflict->flags & flags & IORESOURCE_MUXED) { |
1138 | add_wait_queue(&muxed_resource_wait, &wait); | |
1139 | write_unlock(&resource_lock); | |
1140 | set_current_state(TASK_UNINTERRUPTIBLE); | |
1141 | schedule(); | |
1142 | remove_wait_queue(&muxed_resource_wait, &wait); | |
1143 | write_lock(&resource_lock); | |
1144 | continue; | |
1145 | } | |
c26ec88e | 1146 | /* Uhhuh, that didn't work out.. */ |
ebff7d8f | 1147 | free_resource(res); |
c26ec88e BH |
1148 | res = NULL; |
1149 | break; | |
1da177e4 | 1150 | } |
c26ec88e | 1151 | write_unlock(&resource_lock); |
1da177e4 LT |
1152 | return res; |
1153 | } | |
1da177e4 LT |
1154 | EXPORT_SYMBOL(__request_region); |
1155 | ||
e1ca66d1 RD |
1156 | /** |
1157 | * __release_region - release a previously reserved resource region | |
1158 | * @parent: parent resource descriptor | |
1159 | * @start: resource start address | |
1160 | * @n: resource region size | |
1161 | * | |
1162 | * The described resource region must match a currently busy region. | |
1163 | */ | |
d75fc8bb | 1164 | void __release_region(struct resource *parent, resource_size_t start, |
b69c2e20 | 1165 | resource_size_t n) |
1da177e4 LT |
1166 | { |
1167 | struct resource **p; | |
d75fc8bb | 1168 | resource_size_t end; |
1da177e4 LT |
1169 | |
1170 | p = &parent->child; | |
1171 | end = start + n - 1; | |
1172 | ||
1173 | write_lock(&resource_lock); | |
1174 | ||
1175 | for (;;) { | |
1176 | struct resource *res = *p; | |
1177 | ||
1178 | if (!res) | |
1179 | break; | |
1180 | if (res->start <= start && res->end >= end) { | |
1181 | if (!(res->flags & IORESOURCE_BUSY)) { | |
1182 | p = &res->child; | |
1183 | continue; | |
1184 | } | |
1185 | if (res->start != start || res->end != end) | |
1186 | break; | |
1187 | *p = res->sibling; | |
1188 | write_unlock(&resource_lock); | |
8b6d043b AC |
1189 | if (res->flags & IORESOURCE_MUXED) |
1190 | wake_up(&muxed_resource_wait); | |
ebff7d8f | 1191 | free_resource(res); |
1da177e4 LT |
1192 | return; |
1193 | } | |
1194 | p = &res->sibling; | |
1195 | } | |
1196 | ||
1197 | write_unlock(&resource_lock); | |
1198 | ||
685143ac GKH |
1199 | printk(KERN_WARNING "Trying to free nonexistent resource " |
1200 | "<%016llx-%016llx>\n", (unsigned long long)start, | |
1201 | (unsigned long long)end); | |
1da177e4 | 1202 | } |
1da177e4 LT |
1203 | EXPORT_SYMBOL(__release_region); |
1204 | ||
825f787b TK |
1205 | #ifdef CONFIG_MEMORY_HOTREMOVE |
1206 | /** | |
1207 | * release_mem_region_adjustable - release a previously reserved memory region | |
1208 | * @parent: parent resource descriptor | |
1209 | * @start: resource start address | |
1210 | * @size: resource region size | |
1211 | * | |
1212 | * This interface is intended for memory hot-delete. The requested region | |
1213 | * is released from a currently busy memory resource. The requested region | |
1214 | * must either match exactly or fit into a single busy resource entry. In | |
1215 | * the latter case, the remaining resource is adjusted accordingly. | |
1216 | * Existing children of the busy memory resource must be immutable in the | |
1217 | * request. | |
1218 | * | |
1219 | * Note: | |
1220 | * - Additional release conditions, such as overlapping region, can be | |
1221 | * supported after they are confirmed as valid cases. | |
1222 | * - When a busy memory resource gets split into two entries, the code | |
1223 | * assumes that all children remain in the lower address entry for | |
1224 | * simplicity. Enhance this logic when necessary. | |
1225 | */ | |
1226 | int release_mem_region_adjustable(struct resource *parent, | |
b69c2e20 | 1227 | resource_size_t start, resource_size_t size) |
825f787b TK |
1228 | { |
1229 | struct resource **p; | |
1230 | struct resource *res; | |
1231 | struct resource *new_res; | |
1232 | resource_size_t end; | |
1233 | int ret = -EINVAL; | |
1234 | ||
1235 | end = start + size - 1; | |
1236 | if ((start < parent->start) || (end > parent->end)) | |
1237 | return ret; | |
1238 | ||
ebff7d8f YI |
1239 | /* The alloc_resource() result gets checked later */ |
1240 | new_res = alloc_resource(GFP_KERNEL); | |
825f787b TK |
1241 | |
1242 | p = &parent->child; | |
1243 | write_lock(&resource_lock); | |
1244 | ||
1245 | while ((res = *p)) { | |
1246 | if (res->start >= end) | |
1247 | break; | |
1248 | ||
1249 | /* look for the next resource if it does not fit into */ | |
1250 | if (res->start > start || res->end < end) { | |
1251 | p = &res->sibling; | |
1252 | continue; | |
1253 | } | |
1254 | ||
65c78784 OS |
1255 | /* |
1256 | * All memory regions added from memory-hotplug path have the | |
1257 | * flag IORESOURCE_SYSTEM_RAM. If the resource does not have | |
1258 | * this flag, we know that we are dealing with a resource coming | |
1259 | * from HMM/devm. HMM/devm use another mechanism to add/release | |
1260 | * a resource. This goes via devm_request_mem_region and | |
1261 | * devm_release_mem_region. | |
1262 | * HMM/devm take care to release their resources when they want, | |
1263 | * so if we are dealing with them, let us just back off here. | |
1264 | */ | |
1265 | if (!(res->flags & IORESOURCE_SYSRAM)) { | |
1266 | ret = 0; | |
1267 | break; | |
1268 | } | |
1269 | ||
825f787b TK |
1270 | if (!(res->flags & IORESOURCE_MEM)) |
1271 | break; | |
1272 | ||
1273 | if (!(res->flags & IORESOURCE_BUSY)) { | |
1274 | p = &res->child; | |
1275 | continue; | |
1276 | } | |
1277 | ||
1278 | /* found the target resource; let's adjust accordingly */ | |
1279 | if (res->start == start && res->end == end) { | |
1280 | /* free the whole entry */ | |
1281 | *p = res->sibling; | |
ebff7d8f | 1282 | free_resource(res); |
825f787b TK |
1283 | ret = 0; |
1284 | } else if (res->start == start && res->end != end) { | |
1285 | /* adjust the start */ | |
1286 | ret = __adjust_resource(res, end + 1, | |
1287 | res->end - end); | |
1288 | } else if (res->start != start && res->end == end) { | |
1289 | /* adjust the end */ | |
1290 | ret = __adjust_resource(res, res->start, | |
1291 | start - res->start); | |
1292 | } else { | |
1293 | /* split into two entries */ | |
1294 | if (!new_res) { | |
1295 | ret = -ENOMEM; | |
1296 | break; | |
1297 | } | |
1298 | new_res->name = res->name; | |
1299 | new_res->start = end + 1; | |
1300 | new_res->end = res->end; | |
1301 | new_res->flags = res->flags; | |
43ee493b | 1302 | new_res->desc = res->desc; |
825f787b TK |
1303 | new_res->parent = res->parent; |
1304 | new_res->sibling = res->sibling; | |
1305 | new_res->child = NULL; | |
1306 | ||
1307 | ret = __adjust_resource(res, res->start, | |
1308 | start - res->start); | |
1309 | if (ret) | |
1310 | break; | |
1311 | res->sibling = new_res; | |
1312 | new_res = NULL; | |
1313 | } | |
1314 | ||
1315 | break; | |
1316 | } | |
1317 | ||
1318 | write_unlock(&resource_lock); | |
ebff7d8f | 1319 | free_resource(new_res); |
825f787b TK |
1320 | return ret; |
1321 | } | |
1322 | #endif /* CONFIG_MEMORY_HOTREMOVE */ | |
1323 | ||
9ac7849e TH |
1324 | /* |
1325 | * Managed region resource | |
1326 | */ | |
8d38821c TR |
1327 | static void devm_resource_release(struct device *dev, void *ptr) |
1328 | { | |
1329 | struct resource **r = ptr; | |
1330 | ||
1331 | release_resource(*r); | |
1332 | } | |
1333 | ||
1334 | /** | |
1335 | * devm_request_resource() - request and reserve an I/O or memory resource | |
1336 | * @dev: device for which to request the resource | |
1337 | * @root: root of the resource tree from which to request the resource | |
1338 | * @new: descriptor of the resource to request | |
1339 | * | |
1340 | * This is a device-managed version of request_resource(). There is usually | |
1341 | * no need to release resources requested by this function explicitly since | |
1342 | * that will be taken care of when the device is unbound from its driver. | |
1343 | * If for some reason the resource needs to be released explicitly, because | |
1344 | * of ordering issues for example, drivers must call devm_release_resource() | |
1345 | * rather than the regular release_resource(). | |
1346 | * | |
1347 | * When a conflict is detected between any existing resources and the newly | |
1348 | * requested resource, an error message will be printed. | |
1349 | * | |
1350 | * Returns 0 on success or a negative error code on failure. | |
1351 | */ | |
1352 | int devm_request_resource(struct device *dev, struct resource *root, | |
1353 | struct resource *new) | |
1354 | { | |
1355 | struct resource *conflict, **ptr; | |
1356 | ||
1357 | ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL); | |
1358 | if (!ptr) | |
1359 | return -ENOMEM; | |
1360 | ||
1361 | *ptr = new; | |
1362 | ||
1363 | conflict = request_resource_conflict(root, new); | |
1364 | if (conflict) { | |
1365 | dev_err(dev, "resource collision: %pR conflicts with %s %pR\n", | |
1366 | new, conflict->name, conflict); | |
1367 | devres_free(ptr); | |
1368 | return -EBUSY; | |
1369 | } | |
1370 | ||
1371 | devres_add(dev, ptr); | |
1372 | return 0; | |
1373 | } | |
1374 | EXPORT_SYMBOL(devm_request_resource); | |
1375 | ||
1376 | static int devm_resource_match(struct device *dev, void *res, void *data) | |
1377 | { | |
1378 | struct resource **ptr = res; | |
1379 | ||
1380 | return *ptr == data; | |
1381 | } | |
1382 | ||
1383 | /** | |
1384 | * devm_release_resource() - release a previously requested resource | |
1385 | * @dev: device for which to release the resource | |
1386 | * @new: descriptor of the resource to release | |
1387 | * | |
1388 | * Releases a resource previously requested using devm_request_resource(). | |
1389 | */ | |
1390 | void devm_release_resource(struct device *dev, struct resource *new) | |
1391 | { | |
1392 | WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match, | |
1393 | new)); | |
1394 | } | |
1395 | EXPORT_SYMBOL(devm_release_resource); | |
1396 | ||
9ac7849e TH |
1397 | struct region_devres { |
1398 | struct resource *parent; | |
1399 | resource_size_t start; | |
1400 | resource_size_t n; | |
1401 | }; | |
1402 | ||
1403 | static void devm_region_release(struct device *dev, void *res) | |
1404 | { | |
1405 | struct region_devres *this = res; | |
1406 | ||
1407 | __release_region(this->parent, this->start, this->n); | |
1408 | } | |
1409 | ||
1410 | static int devm_region_match(struct device *dev, void *res, void *match_data) | |
1411 | { | |
1412 | struct region_devres *this = res, *match = match_data; | |
1413 | ||
1414 | return this->parent == match->parent && | |
1415 | this->start == match->start && this->n == match->n; | |
1416 | } | |
1417 | ||
b69c2e20 BP |
1418 | struct resource * |
1419 | __devm_request_region(struct device *dev, struct resource *parent, | |
1420 | resource_size_t start, resource_size_t n, const char *name) | |
9ac7849e TH |
1421 | { |
1422 | struct region_devres *dr = NULL; | |
1423 | struct resource *res; | |
1424 | ||
1425 | dr = devres_alloc(devm_region_release, sizeof(struct region_devres), | |
1426 | GFP_KERNEL); | |
1427 | if (!dr) | |
1428 | return NULL; | |
1429 | ||
1430 | dr->parent = parent; | |
1431 | dr->start = start; | |
1432 | dr->n = n; | |
1433 | ||
e8de1481 | 1434 | res = __request_region(parent, start, n, name, 0); |
9ac7849e TH |
1435 | if (res) |
1436 | devres_add(dev, dr); | |
1437 | else | |
1438 | devres_free(dr); | |
1439 | ||
1440 | return res; | |
1441 | } | |
1442 | EXPORT_SYMBOL(__devm_request_region); | |
1443 | ||
1444 | void __devm_release_region(struct device *dev, struct resource *parent, | |
1445 | resource_size_t start, resource_size_t n) | |
1446 | { | |
1447 | struct region_devres match_data = { parent, start, n }; | |
1448 | ||
1449 | __release_region(parent, start, n); | |
1450 | WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match, | |
1451 | &match_data)); | |
1452 | } | |
1453 | EXPORT_SYMBOL(__devm_release_region); | |
1454 | ||
1da177e4 | 1455 | /* |
ffd2e8df | 1456 | * Reserve I/O ports or memory based on "reserve=" kernel parameter. |
1da177e4 LT |
1457 | */ |
1458 | #define MAXRESERVE 4 | |
1459 | static int __init reserve_setup(char *str) | |
1460 | { | |
1461 | static int reserved; | |
1462 | static struct resource reserve[MAXRESERVE]; | |
1463 | ||
1464 | for (;;) { | |
8bc1ad7d | 1465 | unsigned int io_start, io_num; |
1da177e4 | 1466 | int x = reserved; |
ffd2e8df | 1467 | struct resource *parent; |
1da177e4 | 1468 | |
ffd2e8df | 1469 | if (get_option(&str, &io_start) != 2) |
1da177e4 | 1470 | break; |
ffd2e8df | 1471 | if (get_option(&str, &io_num) == 0) |
1da177e4 LT |
1472 | break; |
1473 | if (x < MAXRESERVE) { | |
1474 | struct resource *res = reserve + x; | |
ffd2e8df BH |
1475 | |
1476 | /* | |
1477 | * If the region starts below 0x10000, we assume it's | |
1478 | * I/O port space; otherwise assume it's memory. | |
1479 | */ | |
1480 | if (io_start < 0x10000) { | |
1481 | res->flags = IORESOURCE_IO; | |
1482 | parent = &ioport_resource; | |
1483 | } else { | |
1484 | res->flags = IORESOURCE_MEM; | |
1485 | parent = &iomem_resource; | |
1486 | } | |
1da177e4 LT |
1487 | res->name = "reserved"; |
1488 | res->start = io_start; | |
1489 | res->end = io_start + io_num - 1; | |
ffd2e8df | 1490 | res->flags |= IORESOURCE_BUSY; |
43ee493b | 1491 | res->desc = IORES_DESC_NONE; |
1da177e4 | 1492 | res->child = NULL; |
ffd2e8df | 1493 | if (request_resource(parent, res) == 0) |
1da177e4 LT |
1494 | reserved = x+1; |
1495 | } | |
1496 | } | |
1497 | return 1; | |
1498 | } | |
1da177e4 | 1499 | __setup("reserve=", reserve_setup); |
379daf62 SS |
1500 | |
1501 | /* | |
1502 | * Check if the requested addr and size spans more than any slot in the | |
1503 | * iomem resource tree. | |
1504 | */ | |
1505 | int iomem_map_sanity_check(resource_size_t addr, unsigned long size) | |
1506 | { | |
1507 | struct resource *p = &iomem_resource; | |
1508 | int err = 0; | |
1509 | loff_t l; | |
1510 | ||
1511 | read_lock(&resource_lock); | |
1512 | for (p = p->child; p ; p = r_next(NULL, p, &l)) { | |
1513 | /* | |
1514 | * We can probably skip the resources without | |
1515 | * IORESOURCE_IO attribute? | |
1516 | */ | |
1517 | if (p->start >= addr + size) | |
1518 | continue; | |
1519 | if (p->end < addr) | |
1520 | continue; | |
d68612b2 SS |
1521 | if (PFN_DOWN(p->start) <= PFN_DOWN(addr) && |
1522 | PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1)) | |
379daf62 | 1523 | continue; |
3ac52669 AV |
1524 | /* |
1525 | * if a resource is "BUSY", it's not a hardware resource | |
1526 | * but a driver mapping of such a resource; we don't want | |
1527 | * to warn for those; some drivers legitimately map only | |
1528 | * partial hardware resources. (example: vesafb) | |
1529 | */ | |
1530 | if (p->flags & IORESOURCE_BUSY) | |
1531 | continue; | |
1532 | ||
e4c72966 | 1533 | printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n", |
13eb8375 IM |
1534 | (unsigned long long)addr, |
1535 | (unsigned long long)(addr + size - 1), | |
e4c72966 | 1536 | p->name, p); |
379daf62 SS |
1537 | err = -1; |
1538 | break; | |
1539 | } | |
1540 | read_unlock(&resource_lock); | |
1541 | ||
1542 | return err; | |
1543 | } | |
e8de1481 AV |
1544 | |
1545 | #ifdef CONFIG_STRICT_DEVMEM | |
1546 | static int strict_iomem_checks = 1; | |
1547 | #else | |
1548 | static int strict_iomem_checks; | |
1549 | #endif | |
1550 | ||
1551 | /* | |
1552 | * check if an address is reserved in the iomem resource tree | |
9825b451 | 1553 | * returns true if reserved, false if not reserved. |
e8de1481 | 1554 | */ |
9825b451 | 1555 | bool iomem_is_exclusive(u64 addr) |
e8de1481 AV |
1556 | { |
1557 | struct resource *p = &iomem_resource; | |
9825b451 | 1558 | bool err = false; |
e8de1481 AV |
1559 | loff_t l; |
1560 | int size = PAGE_SIZE; | |
1561 | ||
1562 | if (!strict_iomem_checks) | |
9825b451 | 1563 | return false; |
e8de1481 AV |
1564 | |
1565 | addr = addr & PAGE_MASK; | |
1566 | ||
1567 | read_lock(&resource_lock); | |
1568 | for (p = p->child; p ; p = r_next(NULL, p, &l)) { | |
1569 | /* | |
1570 | * We can probably skip the resources without | |
1571 | * IORESOURCE_IO attribute? | |
1572 | */ | |
1573 | if (p->start >= addr + size) | |
1574 | break; | |
1575 | if (p->end < addr) | |
1576 | continue; | |
90a545e9 DW |
1577 | /* |
1578 | * A resource is exclusive if IORESOURCE_EXCLUSIVE is set | |
1579 | * or CONFIG_IO_STRICT_DEVMEM is enabled and the | |
1580 | * resource is busy. | |
1581 | */ | |
1582 | if ((p->flags & IORESOURCE_BUSY) == 0) | |
1583 | continue; | |
1584 | if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM) | |
1585 | || p->flags & IORESOURCE_EXCLUSIVE) { | |
9825b451 | 1586 | err = true; |
e8de1481 AV |
1587 | break; |
1588 | } | |
1589 | } | |
1590 | read_unlock(&resource_lock); | |
1591 | ||
1592 | return err; | |
1593 | } | |
1594 | ||
90e97820 JL |
1595 | struct resource_entry *resource_list_create_entry(struct resource *res, |
1596 | size_t extra_size) | |
1597 | { | |
1598 | struct resource_entry *entry; | |
1599 | ||
1600 | entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL); | |
1601 | if (entry) { | |
1602 | INIT_LIST_HEAD(&entry->node); | |
1603 | entry->res = res ? res : &entry->__res; | |
1604 | } | |
1605 | ||
1606 | return entry; | |
1607 | } | |
1608 | EXPORT_SYMBOL(resource_list_create_entry); | |
1609 | ||
1610 | void resource_list_free(struct list_head *head) | |
1611 | { | |
1612 | struct resource_entry *entry, *tmp; | |
1613 | ||
1614 | list_for_each_entry_safe(entry, tmp, head, node) | |
1615 | resource_list_destroy_entry(entry); | |
1616 | } | |
1617 | EXPORT_SYMBOL(resource_list_free); | |
1618 | ||
e8de1481 AV |
1619 | static int __init strict_iomem(char *str) |
1620 | { | |
1621 | if (strstr(str, "relaxed")) | |
1622 | strict_iomem_checks = 0; | |
1623 | if (strstr(str, "strict")) | |
1624 | strict_iomem_checks = 1; | |
1625 | return 1; | |
1626 | } | |
1627 | ||
1628 | __setup("iomem=", strict_iomem); |