]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/resource.c
[PATCH] kernel-doc for kernel/dma.c
[mirror_ubuntu-artful-kernel.git] / kernel / resource.c
CommitLineData
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
LT
10#include <linux/module.h>
11#include <linux/sched.h>
12#include <linux/errno.h>
13#include <linux/ioport.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/fs.h>
18#include <linux/proc_fs.h>
19#include <linux/seq_file.h>
20#include <asm/io.h>
21
22
23struct resource ioport_resource = {
24 .name = "PCI IO",
6550e07f 25 .start = 0,
1da177e4
LT
26 .end = IO_SPACE_LIMIT,
27 .flags = IORESOURCE_IO,
28};
1da177e4
LT
29EXPORT_SYMBOL(ioport_resource);
30
31struct resource iomem_resource = {
32 .name = "PCI mem",
6550e07f
GKH
33 .start = 0,
34 .end = -1,
1da177e4
LT
35 .flags = IORESOURCE_MEM,
36};
1da177e4
LT
37EXPORT_SYMBOL(iomem_resource);
38
39static DEFINE_RWLOCK(resource_lock);
40
41#ifdef CONFIG_PROC_FS
42
43enum { MAX_IORES_LEVEL = 5 };
44
45static void *r_next(struct seq_file *m, void *v, loff_t *pos)
46{
47 struct resource *p = v;
48 (*pos)++;
49 if (p->child)
50 return p->child;
51 while (!p->sibling && p->parent)
52 p = p->parent;
53 return p->sibling;
54}
55
56static void *r_start(struct seq_file *m, loff_t *pos)
57 __acquires(resource_lock)
58{
59 struct resource *p = m->private;
60 loff_t l = 0;
61 read_lock(&resource_lock);
62 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
63 ;
64 return p;
65}
66
67static void r_stop(struct seq_file *m, void *v)
68 __releases(resource_lock)
69{
70 read_unlock(&resource_lock);
71}
72
73static int r_show(struct seq_file *m, void *v)
74{
75 struct resource *root = m->private;
76 struct resource *r = v, *p;
77 int width = root->end < 0x10000 ? 4 : 8;
78 int depth;
79
80 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
81 if (p->parent == root)
82 break;
685143ac 83 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
1da177e4 84 depth * 2, "",
685143ac
GKH
85 width, (unsigned long long) r->start,
86 width, (unsigned long long) r->end,
1da177e4
LT
87 r->name ? r->name : "<BAD>");
88 return 0;
89}
90
91static struct seq_operations resource_op = {
92 .start = r_start,
93 .next = r_next,
94 .stop = r_stop,
95 .show = r_show,
96};
97
98static int ioports_open(struct inode *inode, struct file *file)
99{
100 int res = seq_open(file, &resource_op);
101 if (!res) {
102 struct seq_file *m = file->private_data;
103 m->private = &ioport_resource;
104 }
105 return res;
106}
107
108static int iomem_open(struct inode *inode, struct file *file)
109{
110 int res = seq_open(file, &resource_op);
111 if (!res) {
112 struct seq_file *m = file->private_data;
113 m->private = &iomem_resource;
114 }
115 return res;
116}
117
118static struct file_operations proc_ioports_operations = {
119 .open = ioports_open,
120 .read = seq_read,
121 .llseek = seq_lseek,
122 .release = seq_release,
123};
124
125static struct file_operations proc_iomem_operations = {
126 .open = iomem_open,
127 .read = seq_read,
128 .llseek = seq_lseek,
129 .release = seq_release,
130};
131
132static int __init ioresources_init(void)
133{
134 struct proc_dir_entry *entry;
135
136 entry = create_proc_entry("ioports", 0, NULL);
137 if (entry)
138 entry->proc_fops = &proc_ioports_operations;
139 entry = create_proc_entry("iomem", 0, NULL);
140 if (entry)
141 entry->proc_fops = &proc_iomem_operations;
142 return 0;
143}
144__initcall(ioresources_init);
145
146#endif /* CONFIG_PROC_FS */
147
148/* Return the conflict entry if you can't request it */
149static struct resource * __request_resource(struct resource *root, struct resource *new)
150{
d75fc8bb
GKH
151 resource_size_t start = new->start;
152 resource_size_t end = new->end;
1da177e4
LT
153 struct resource *tmp, **p;
154
155 if (end < start)
156 return root;
157 if (start < root->start)
158 return root;
159 if (end > root->end)
160 return root;
161 p = &root->child;
162 for (;;) {
163 tmp = *p;
164 if (!tmp || tmp->start > end) {
165 new->sibling = tmp;
166 *p = new;
167 new->parent = root;
168 return NULL;
169 }
170 p = &tmp->sibling;
171 if (tmp->end < start)
172 continue;
173 return tmp;
174 }
175}
176
177static int __release_resource(struct resource *old)
178{
179 struct resource *tmp, **p;
180
181 p = &old->parent->child;
182 for (;;) {
183 tmp = *p;
184 if (!tmp)
185 break;
186 if (tmp == old) {
187 *p = tmp->sibling;
188 old->parent = NULL;
189 return 0;
190 }
191 p = &tmp->sibling;
192 }
193 return -EINVAL;
194}
195
196int request_resource(struct resource *root, struct resource *new)
197{
198 struct resource *conflict;
199
200 write_lock(&resource_lock);
201 conflict = __request_resource(root, new);
202 write_unlock(&resource_lock);
203 return conflict ? -EBUSY : 0;
204}
205
206EXPORT_SYMBOL(request_resource);
207
208struct resource *____request_resource(struct resource *root, struct resource *new)
209{
210 struct resource *conflict;
211
212 write_lock(&resource_lock);
213 conflict = __request_resource(root, new);
214 write_unlock(&resource_lock);
215 return conflict;
216}
217
218EXPORT_SYMBOL(____request_resource);
219
220int release_resource(struct resource *old)
221{
222 int retval;
223
224 write_lock(&resource_lock);
225 retval = __release_resource(old);
226 write_unlock(&resource_lock);
227 return retval;
228}
229
230EXPORT_SYMBOL(release_resource);
231
2842f114
KH
232#ifdef CONFIG_MEMORY_HOTPLUG
233/*
234 * Finds the lowest memory reosurce exists within [res->start.res->end)
235 * the caller must specify res->start, res->end, res->flags.
236 * If found, returns 0, res is overwritten, if not found, returns -1.
237 */
238int find_next_system_ram(struct resource *res)
239{
240 resource_size_t start, end;
241 struct resource *p;
242
243 BUG_ON(!res);
244
245 start = res->start;
246 end = res->end;
58c1b5b0 247 BUG_ON(start >= end);
2842f114
KH
248
249 read_lock(&resource_lock);
250 for (p = iomem_resource.child; p ; p = p->sibling) {
251 /* system ram is just marked as IORESOURCE_MEM */
252 if (p->flags != res->flags)
253 continue;
254 if (p->start > end) {
255 p = NULL;
256 break;
257 }
58c1b5b0 258 if ((p->end >= start) && (p->start < end))
2842f114
KH
259 break;
260 }
261 read_unlock(&resource_lock);
262 if (!p)
263 return -1;
264 /* copy data */
0f04ab5e
KH
265 if (res->start < p->start)
266 res->start = p->start;
267 if (res->end > p->end)
268 res->end = p->end;
2842f114
KH
269 return 0;
270}
271#endif
272
1da177e4
LT
273/*
274 * Find empty slot in the resource tree given range and alignment.
275 */
276static int find_resource(struct resource *root, struct resource *new,
d75fc8bb
GKH
277 resource_size_t size, resource_size_t min,
278 resource_size_t max, resource_size_t align,
1da177e4 279 void (*alignf)(void *, struct resource *,
d75fc8bb 280 resource_size_t, resource_size_t),
1da177e4
LT
281 void *alignf_data)
282{
283 struct resource *this = root->child;
284
285 new->start = root->start;
286 /*
287 * Skip past an allocated resource that starts at 0, since the assignment
288 * of this->start - 1 to new->end below would cause an underflow.
289 */
290 if (this && this->start == 0) {
291 new->start = this->end + 1;
292 this = this->sibling;
293 }
294 for(;;) {
295 if (this)
296 new->end = this->start - 1;
297 else
298 new->end = root->end;
299 if (new->start < min)
300 new->start = min;
301 if (new->end > max)
302 new->end = max;
8c0e33c1 303 new->start = ALIGN(new->start, align);
1da177e4
LT
304 if (alignf)
305 alignf(alignf_data, new, size, align);
b52402c7 306 if (new->start < new->end && new->end - new->start >= size - 1) {
1da177e4
LT
307 new->end = new->start + size - 1;
308 return 0;
309 }
310 if (!this)
311 break;
312 new->start = this->end + 1;
313 this = this->sibling;
314 }
315 return -EBUSY;
316}
317
318/*
319 * Allocate empty slot in the resource tree given range and alignment.
320 */
321int allocate_resource(struct resource *root, struct resource *new,
d75fc8bb
GKH
322 resource_size_t size, resource_size_t min,
323 resource_size_t max, resource_size_t align,
1da177e4 324 void (*alignf)(void *, struct resource *,
d75fc8bb 325 resource_size_t, resource_size_t),
1da177e4
LT
326 void *alignf_data)
327{
328 int err;
329
330 write_lock(&resource_lock);
331 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
332 if (err >= 0 && __request_resource(root, new))
333 err = -EBUSY;
334 write_unlock(&resource_lock);
335 return err;
336}
337
338EXPORT_SYMBOL(allocate_resource);
339
340/**
341 * insert_resource - Inserts a resource in the resource tree
342 * @parent: parent of the new resource
343 * @new: new resource to insert
344 *
345 * Returns 0 on success, -EBUSY if the resource can't be inserted.
346 *
d33b6fba 347 * This function is equivalent to request_resource when no conflict
1da177e4
LT
348 * happens. If a conflict happens, and the conflicting resources
349 * entirely fit within the range of the new resource, then the new
d33b6fba
MW
350 * resource is inserted and the conflicting resources become children of
351 * the new resource.
1da177e4
LT
352 */
353int insert_resource(struct resource *parent, struct resource *new)
354{
355 int result;
356 struct resource *first, *next;
357
358 write_lock(&resource_lock);
d33b6fba
MW
359
360 for (;; parent = first) {
361 result = 0;
362 first = __request_resource(parent, new);
363 if (!first)
364 goto out;
365
366 result = -EBUSY;
367 if (first == parent)
368 goto out;
369
370 if ((first->start > new->start) || (first->end < new->end))
371 break;
372 if ((first->start == new->start) && (first->end == new->end))
373 break;
1da177e4
LT
374 }
375
376 for (next = first; ; next = next->sibling) {
377 /* Partial overlap? Bad, and unfixable */
378 if (next->start < new->start || next->end > new->end)
379 goto out;
380 if (!next->sibling)
381 break;
382 if (next->sibling->start > new->end)
383 break;
384 }
385
386 result = 0;
387
388 new->parent = parent;
389 new->sibling = next->sibling;
390 new->child = first;
391
392 next->sibling = NULL;
393 for (next = first; next; next = next->sibling)
394 next->parent = new;
395
396 if (parent->child == first) {
397 parent->child = new;
398 } else {
399 next = parent->child;
400 while (next->sibling != first)
401 next = next->sibling;
402 next->sibling = new;
403 }
404
405 out:
406 write_unlock(&resource_lock);
407 return result;
408}
409
1da177e4
LT
410/*
411 * Given an existing resource, change its start and size to match the
412 * arguments. Returns -EBUSY if it can't fit. Existing children of
413 * the resource are assumed to be immutable.
414 */
d75fc8bb 415int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
1da177e4
LT
416{
417 struct resource *tmp, *parent = res->parent;
d75fc8bb 418 resource_size_t end = start + size - 1;
1da177e4
LT
419 int result = -EBUSY;
420
421 write_lock(&resource_lock);
422
423 if ((start < parent->start) || (end > parent->end))
424 goto out;
425
426 for (tmp = res->child; tmp; tmp = tmp->sibling) {
427 if ((tmp->start < start) || (tmp->end > end))
428 goto out;
429 }
430
431 if (res->sibling && (res->sibling->start <= end))
432 goto out;
433
434 tmp = parent->child;
435 if (tmp != res) {
436 while (tmp->sibling != res)
437 tmp = tmp->sibling;
438 if (start <= tmp->end)
439 goto out;
440 }
441
442 res->start = start;
443 res->end = end;
444 result = 0;
445
446 out:
447 write_unlock(&resource_lock);
448 return result;
449}
450
451EXPORT_SYMBOL(adjust_resource);
452
453/*
454 * This is compatibility stuff for IO resources.
455 *
456 * Note how this, unlike the above, knows about
457 * the IO flag meanings (busy etc).
458 *
459 * Request-region creates a new busy region.
460 *
461 * Check-region returns non-zero if the area is already busy
462 *
463 * Release-region releases a matching busy region.
464 */
d75fc8bb
GKH
465struct resource * __request_region(struct resource *parent,
466 resource_size_t start, resource_size_t n,
467 const char *name)
1da177e4 468{
dd392710 469 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
1da177e4
LT
470
471 if (res) {
1da177e4
LT
472 res->name = name;
473 res->start = start;
474 res->end = start + n - 1;
475 res->flags = IORESOURCE_BUSY;
476
477 write_lock(&resource_lock);
478
479 for (;;) {
480 struct resource *conflict;
481
482 conflict = __request_resource(parent, res);
483 if (!conflict)
484 break;
485 if (conflict != parent) {
486 parent = conflict;
487 if (!(conflict->flags & IORESOURCE_BUSY))
488 continue;
489 }
490
491 /* Uhhuh, that didn't work out.. */
492 kfree(res);
493 res = NULL;
494 break;
495 }
496 write_unlock(&resource_lock);
497 }
498 return res;
499}
500
501EXPORT_SYMBOL(__request_region);
502
d75fc8bb
GKH
503int __check_region(struct resource *parent, resource_size_t start,
504 resource_size_t n)
1da177e4
LT
505{
506 struct resource * res;
507
508 res = __request_region(parent, start, n, "check-region");
509 if (!res)
510 return -EBUSY;
511
512 release_resource(res);
513 kfree(res);
514 return 0;
515}
516
517EXPORT_SYMBOL(__check_region);
518
d75fc8bb
GKH
519void __release_region(struct resource *parent, resource_size_t start,
520 resource_size_t n)
1da177e4
LT
521{
522 struct resource **p;
d75fc8bb 523 resource_size_t end;
1da177e4
LT
524
525 p = &parent->child;
526 end = start + n - 1;
527
528 write_lock(&resource_lock);
529
530 for (;;) {
531 struct resource *res = *p;
532
533 if (!res)
534 break;
535 if (res->start <= start && res->end >= end) {
536 if (!(res->flags & IORESOURCE_BUSY)) {
537 p = &res->child;
538 continue;
539 }
540 if (res->start != start || res->end != end)
541 break;
542 *p = res->sibling;
543 write_unlock(&resource_lock);
544 kfree(res);
545 return;
546 }
547 p = &res->sibling;
548 }
549
550 write_unlock(&resource_lock);
551
685143ac
GKH
552 printk(KERN_WARNING "Trying to free nonexistent resource "
553 "<%016llx-%016llx>\n", (unsigned long long)start,
554 (unsigned long long)end);
1da177e4
LT
555}
556
557EXPORT_SYMBOL(__release_region);
558
559/*
560 * Called from init/main.c to reserve IO ports.
561 */
562#define MAXRESERVE 4
563static int __init reserve_setup(char *str)
564{
565 static int reserved;
566 static struct resource reserve[MAXRESERVE];
567
568 for (;;) {
569 int io_start, io_num;
570 int x = reserved;
571
572 if (get_option (&str, &io_start) != 2)
573 break;
574 if (get_option (&str, &io_num) == 0)
575 break;
576 if (x < MAXRESERVE) {
577 struct resource *res = reserve + x;
578 res->name = "reserved";
579 res->start = io_start;
580 res->end = io_start + io_num - 1;
581 res->flags = IORESOURCE_BUSY;
582 res->child = NULL;
583 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
584 reserved = x+1;
585 }
586 }
587 return 1;
588}
589
590__setup("reserve=", reserve_setup);