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