2 * linux/kernel/resource.c
4 * Copyright (C) 1999 Linus Torvalds
6 * Arbitrary resource management.
9 #include <linux/sched.h>
10 #include <linux/errno.h>
11 #include <linux/ioport.h>
12 #include <linux/init.h>
13 #include <linux/malloc.h>
15 struct resource ioport_resource
= { "PCI IO", 0x0000, 0xFFFF };
16 struct resource iomem_resource
= { "PCI mem", 0x00000000, 0xFFFFFFFF };
19 * This generates reports for /proc/ioports and /proc/memory
21 static char * do_resource_list(struct resource
*entry
, const char *fmt
, int offset
, char *buf
, char *end
)
27 const char *name
= entry
->name
;
28 unsigned long from
, to
;
30 if ((int) (end
-buf
) < 80)
38 buf
+= sprintf(buf
, fmt
+ offset
, from
, to
, name
);
40 buf
= do_resource_list(entry
->child
, fmt
, offset
-2, buf
, end
);
41 entry
= entry
->sibling
;
47 int get_resource_list(struct resource
*root
, char *buf
, int size
)
51 fmt
= " %08lx-%08lx : %s\n";
52 if (root
== &ioport_resource
)
53 fmt
= " %04lx-%04lx : %s\n";
54 return do_resource_list(root
->child
, fmt
, 8, buf
, buf
+ size
) - buf
;
57 int request_resource(struct resource
*root
, struct resource
*new)
59 unsigned long start
= new->start
;
60 unsigned long end
= new->end
;
61 struct resource
*tmp
, **p
;
65 if (start
< root
->start
)
72 if (!tmp
|| tmp
->start
> end
) {
85 int release_resource(struct resource
*old
)
87 struct resource
*tmp
, **p
;
89 p
= &old
->parent
->child
;
104 struct resource
* __request_region(struct resource
*parent
, unsigned long start
, unsigned long n
, const char *name
)
106 struct resource
*res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
109 memset(res
, 0, sizeof(*res
));
112 res
->end
= start
+ n
- 1;
113 if (request_resource(parent
, res
) != 0) {
122 * Compatibility cruft.
124 * Check-region returns non-zero if something already exists.
126 * Release-region releases an anonymous region that matches
129 int __check_region(struct resource
*parent
, unsigned long start
, unsigned long n
)
131 struct resource
* res
;
133 res
= __request_region(parent
, start
, n
, "check-region");
137 release_resource(res
);
142 void __release_region(struct resource
*parent
, unsigned long start
, unsigned long n
)
151 struct resource
*res
= *p
;
155 if (res
->start
== start
&& res
->end
== end
) {
165 * Called from init/main.c to reserve IO ports.
168 void __init
reserve_setup(char *str
, int *ints
)
171 static int reserved
= 0;
172 static struct resource reserve
[MAXRESERVE
];
174 for (i
= 1; i
< ints
[0]; i
+= 2) {
176 if (x
< MAXRESERVE
) {
177 struct resource
*res
= reserve
+ x
;
178 res
->name
= "reserved";
179 res
->start
= ints
[i
];
180 res
->end
= res
->start
+ ints
[i
] - 1;
182 if (request_resource(&ioport_resource
, res
) == 0)