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