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