[PATCH] catch valid mem range at onlining memory
[safe/jmp/linux-2.6] / kernel / resource.c
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
24 struct resource ioport_resource = {
25         .name   = "PCI IO",
26         .start  = 0x0000,
27         .end    = IO_SPACE_LIMIT,
28         .flags  = IORESOURCE_IO,
29 };
30
31 EXPORT_SYMBOL(ioport_resource);
32
33 struct resource iomem_resource = {
34         .name   = "PCI mem",
35         .start  = 0UL,
36         .end    = ~0UL,
37         .flags  = IORESOURCE_MEM,
38 };
39
40 EXPORT_SYMBOL(iomem_resource);
41
42 static DEFINE_RWLOCK(resource_lock);
43
44 #ifdef CONFIG_PROC_FS
45
46 enum { MAX_IORES_LEVEL = 5 };
47
48 static 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
59 static 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
70 static void r_stop(struct seq_file *m, void *v)
71         __releases(resource_lock)
72 {
73         read_unlock(&resource_lock);
74 }
75
76 static 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;
86         seq_printf(m, "%*s%0*lx-%0*lx : %s\n",
87                         depth * 2, "",
88                         width, r->start,
89                         width, r->end,
90                         r->name ? r->name : "<BAD>");
91         return 0;
92 }
93
94 static struct seq_operations resource_op = {
95         .start  = r_start,
96         .next   = r_next,
97         .stop   = r_stop,
98         .show   = r_show,
99 };
100
101 static 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
111 static 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
121 static struct file_operations proc_ioports_operations = {
122         .open           = ioports_open,
123         .read           = seq_read,
124         .llseek         = seq_lseek,
125         .release        = seq_release,
126 };
127
128 static struct file_operations proc_iomem_operations = {
129         .open           = iomem_open,
130         .read           = seq_read,
131         .llseek         = seq_lseek,
132         .release        = seq_release,
133 };
134
135 static 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 */
152 static struct resource * __request_resource(struct resource *root, struct resource *new)
153 {
154         unsigned long start = new->start;
155         unsigned long end = new->end;
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
180 static 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
199 int 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
209 EXPORT_SYMBOL(request_resource);
210
211 struct 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
221 EXPORT_SYMBOL(____request_resource);
222
223 int 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
233 EXPORT_SYMBOL(release_resource);
234
235 #ifdef CONFIG_MEMORY_HOTPLUG
236 /*
237  * Finds the lowest memory reosurce exists within [res->start.res->end)
238  * the caller must specify res->start, res->end, res->flags.
239  * If found, returns 0, res is overwritten, if not found, returns -1.
240  */
241 int find_next_system_ram(struct resource *res)
242 {
243         resource_size_t start, end;
244         struct resource *p;
245
246         BUG_ON(!res);
247
248         start = res->start;
249         end = res->end;
250
251         read_lock(&resource_lock);
252         for (p = iomem_resource.child; p ; p = p->sibling) {
253                 /* system ram is just marked as IORESOURCE_MEM */
254                 if (p->flags != res->flags)
255                         continue;
256                 if (p->start > end) {
257                         p = NULL;
258                         break;
259                 }
260                 if (p->start >= start)
261                         break;
262         }
263         read_unlock(&resource_lock);
264         if (!p)
265                 return -1;
266         /* copy data */
267         res->start = p->start;
268         res->end = p->end;
269         return 0;
270 }
271 #endif
272
273 /*
274  * Find empty slot in the resource tree given range and alignment.
275  */
276 static int find_resource(struct resource *root, struct resource *new,
277                          unsigned long size,
278                          unsigned long min, unsigned long max,
279                          unsigned long align,
280                          void (*alignf)(void *, struct resource *,
281                                         unsigned long, unsigned long),
282                          void *alignf_data)
283 {
284         struct resource *this = root->child;
285
286         new->start = root->start;
287         /*
288          * Skip past an allocated resource that starts at 0, since the assignment
289          * of this->start - 1 to new->end below would cause an underflow.
290          */
291         if (this && this->start == 0) {
292                 new->start = this->end + 1;
293                 this = this->sibling;
294         }
295         for(;;) {
296                 if (this)
297                         new->end = this->start - 1;
298                 else
299                         new->end = root->end;
300                 if (new->start < min)
301                         new->start = min;
302                 if (new->end > max)
303                         new->end = max;
304                 new->start = ALIGN(new->start, align);
305                 if (alignf)
306                         alignf(alignf_data, new, size, align);
307                 if (new->start < new->end && new->end - new->start >= size - 1) {
308                         new->end = new->start + size - 1;
309                         return 0;
310                 }
311                 if (!this)
312                         break;
313                 new->start = this->end + 1;
314                 this = this->sibling;
315         }
316         return -EBUSY;
317 }
318
319 /*
320  * Allocate empty slot in the resource tree given range and alignment.
321  */
322 int allocate_resource(struct resource *root, struct resource *new,
323                       unsigned long size,
324                       unsigned long min, unsigned long max,
325                       unsigned long align,
326                       void (*alignf)(void *, struct resource *,
327                                      unsigned long, unsigned long),
328                       void *alignf_data)
329 {
330         int err;
331
332         write_lock(&resource_lock);
333         err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
334         if (err >= 0 && __request_resource(root, new))
335                 err = -EBUSY;
336         write_unlock(&resource_lock);
337         return err;
338 }
339
340 EXPORT_SYMBOL(allocate_resource);
341
342 /**
343  * insert_resource - Inserts a resource in the resource tree
344  * @parent: parent of the new resource
345  * @new: new resource to insert
346  *
347  * Returns 0 on success, -EBUSY if the resource can't be inserted.
348  *
349  * This function is equivalent of request_resource when no conflict
350  * happens. If a conflict happens, and the conflicting resources
351  * entirely fit within the range of the new resource, then the new
352  * resource is inserted and the conflicting resources become childs of
353  * the new resource.  Otherwise the new resource becomes the child of
354  * the conflicting resource
355  */
356 int insert_resource(struct resource *parent, struct resource *new)
357 {
358         int result;
359         struct resource *first, *next;
360
361         write_lock(&resource_lock);
362  begin:
363         result = 0;
364         first = __request_resource(parent, new);
365         if (!first)
366                 goto out;
367
368         result = -EBUSY;
369         if (first == parent)
370                 goto out;
371
372         /* Resource fully contained by the clashing resource? Recurse into it */
373         if (first->start <= new->start && first->end >= new->end) {
374                 parent = first;
375                 goto begin;
376         }
377
378         for (next = first; ; next = next->sibling) {
379                 /* Partial overlap? Bad, and unfixable */
380                 if (next->start < new->start || next->end > new->end)
381                         goto out;
382                 if (!next->sibling)
383                         break;
384                 if (next->sibling->start > new->end)
385                         break;
386         }
387
388         result = 0;
389
390         new->parent = parent;
391         new->sibling = next->sibling;
392         new->child = first;
393
394         next->sibling = NULL;
395         for (next = first; next; next = next->sibling)
396                 next->parent = new;
397
398         if (parent->child == first) {
399                 parent->child = new;
400         } else {
401                 next = parent->child;
402                 while (next->sibling != first)
403                         next = next->sibling;
404                 next->sibling = new;
405         }
406
407  out:
408         write_unlock(&resource_lock);
409         return result;
410 }
411
412 EXPORT_SYMBOL(insert_resource);
413
414 /*
415  * Given an existing resource, change its start and size to match the
416  * arguments.  Returns -EBUSY if it can't fit.  Existing children of
417  * the resource are assumed to be immutable.
418  */
419 int adjust_resource(struct resource *res, unsigned long start, unsigned long size)
420 {
421         struct resource *tmp, *parent = res->parent;
422         unsigned long end = start + size - 1;
423         int result = -EBUSY;
424
425         write_lock(&resource_lock);
426
427         if ((start < parent->start) || (end > parent->end))
428                 goto out;
429
430         for (tmp = res->child; tmp; tmp = tmp->sibling) {
431                 if ((tmp->start < start) || (tmp->end > end))
432                         goto out;
433         }
434
435         if (res->sibling && (res->sibling->start <= end))
436                 goto out;
437
438         tmp = parent->child;
439         if (tmp != res) {
440                 while (tmp->sibling != res)
441                         tmp = tmp->sibling;
442                 if (start <= tmp->end)
443                         goto out;
444         }
445
446         res->start = start;
447         res->end = end;
448         result = 0;
449
450  out:
451         write_unlock(&resource_lock);
452         return result;
453 }
454
455 EXPORT_SYMBOL(adjust_resource);
456
457 /*
458  * This is compatibility stuff for IO resources.
459  *
460  * Note how this, unlike the above, knows about
461  * the IO flag meanings (busy etc).
462  *
463  * Request-region creates a new busy region.
464  *
465  * Check-region returns non-zero if the area is already busy
466  *
467  * Release-region releases a matching busy region.
468  */
469 struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name)
470 {
471         struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
472
473         if (res) {
474                 res->name = name;
475                 res->start = start;
476                 res->end = start + n - 1;
477                 res->flags = IORESOURCE_BUSY;
478
479                 write_lock(&resource_lock);
480
481                 for (;;) {
482                         struct resource *conflict;
483
484                         conflict = __request_resource(parent, res);
485                         if (!conflict)
486                                 break;
487                         if (conflict != parent) {
488                                 parent = conflict;
489                                 if (!(conflict->flags & IORESOURCE_BUSY))
490                                         continue;
491                         }
492
493                         /* Uhhuh, that didn't work out.. */
494                         kfree(res);
495                         res = NULL;
496                         break;
497                 }
498                 write_unlock(&resource_lock);
499         }
500         return res;
501 }
502
503 EXPORT_SYMBOL(__request_region);
504
505 int __check_region(struct resource *parent, unsigned long start, unsigned long n)
506 {
507         struct resource * res;
508
509         res = __request_region(parent, start, n, "check-region");
510         if (!res)
511                 return -EBUSY;
512
513         release_resource(res);
514         kfree(res);
515         return 0;
516 }
517
518 EXPORT_SYMBOL(__check_region);
519
520 void __release_region(struct resource *parent, unsigned long start, unsigned long n)
521 {
522         struct resource **p;
523         unsigned long end;
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
552         printk(KERN_WARNING "Trying to free nonexistent resource <%08lx-%08lx>\n", start, end);
553 }
554
555 EXPORT_SYMBOL(__release_region);
556
557 /*
558  * Called from init/main.c to reserve IO ports.
559  */
560 #define MAXRESERVE 4
561 static int __init reserve_setup(char *str)
562 {
563         static int reserved;
564         static struct resource reserve[MAXRESERVE];
565
566         for (;;) {
567                 int io_start, io_num;
568                 int x = reserved;
569
570                 if (get_option (&str, &io_start) != 2)
571                         break;
572                 if (get_option (&str, &io_num)   == 0)
573                         break;
574                 if (x < MAXRESERVE) {
575                         struct resource *res = reserve + x;
576                         res->name = "reserved";
577                         res->start = io_start;
578                         res->end = io_start + io_num - 1;
579                         res->flags = IORESOURCE_BUSY;
580                         res->child = NULL;
581                         if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
582                                 reserved = x+1;
583                 }
584         }
585         return 1;
586 }
587
588 __setup("reserve=", reserve_setup);