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