ACPI: video: Remove unneeded acpi_handle from driver.
[pandora-kernel.git] / 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  = 0,
27         .end    = IO_SPACE_LIMIT,
28         .flags  = IORESOURCE_IO,
29 };
30 EXPORT_SYMBOL(ioport_resource);
31
32 struct resource iomem_resource = {
33         .name   = "PCI mem",
34         .start  = 0,
35         .end    = -1,
36         .flags  = IORESOURCE_MEM,
37 };
38 EXPORT_SYMBOL(iomem_resource);
39
40 static DEFINE_RWLOCK(resource_lock);
41
42 #ifdef CONFIG_PROC_FS
43
44 enum { MAX_IORES_LEVEL = 5 };
45
46 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
47 {
48         struct resource *p = v;
49         (*pos)++;
50         if (p->child)
51                 return p->child;
52         while (!p->sibling && p->parent)
53                 p = p->parent;
54         return p->sibling;
55 }
56
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;
84         seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
85                         depth * 2, "",
86                         width, (unsigned long long) r->start,
87                         width, (unsigned long long) r->end,
88                         r->name ? r->name : "<BAD>");
89         return 0;
90 }
91
92 static struct seq_operations resource_op = {
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
119 static struct file_operations proc_ioports_operations = {
120         .open           = ioports_open,
121         .read           = seq_read,
122         .llseek         = seq_lseek,
123         .release        = seq_release,
124 };
125
126 static struct file_operations proc_iomem_operations = {
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 {
135         struct proc_dir_entry *entry;
136
137         entry = create_proc_entry("ioports", 0, NULL);
138         if (entry)
139                 entry->proc_fops = &proc_ioports_operations;
140         entry = create_proc_entry("iomem", 0, NULL);
141         if (entry)
142                 entry->proc_fops = &proc_iomem_operations;
143         return 0;
144 }
145 __initcall(ioresources_init);
146
147 #endif /* CONFIG_PROC_FS */
148
149 /* Return the conflict entry if you can't request it */
150 static struct resource * __request_resource(struct resource *root, struct resource *new)
151 {
152         resource_size_t start = new->start;
153         resource_size_t end = new->end;
154         struct resource *tmp, **p;
155
156         if (end < start)
157                 return root;
158         if (start < root->start)
159                 return root;
160         if (end > root->end)
161                 return root;
162         p = &root->child;
163         for (;;) {
164                 tmp = *p;
165                 if (!tmp || tmp->start > end) {
166                         new->sibling = tmp;
167                         *p = new;
168                         new->parent = root;
169                         return NULL;
170                 }
171                 p = &tmp->sibling;
172                 if (tmp->end < start)
173                         continue;
174                 return tmp;
175         }
176 }
177
178 static int __release_resource(struct resource *old)
179 {
180         struct resource *tmp, **p;
181
182         p = &old->parent->child;
183         for (;;) {
184                 tmp = *p;
185                 if (!tmp)
186                         break;
187                 if (tmp == old) {
188                         *p = tmp->sibling;
189                         old->parent = NULL;
190                         return 0;
191                 }
192                 p = &tmp->sibling;
193         }
194         return -EINVAL;
195 }
196
197 int request_resource(struct resource *root, struct resource *new)
198 {
199         struct resource *conflict;
200
201         write_lock(&resource_lock);
202         conflict = __request_resource(root, new);
203         write_unlock(&resource_lock);
204         return conflict ? -EBUSY : 0;
205 }
206
207 EXPORT_SYMBOL(request_resource);
208
209 struct resource *____request_resource(struct resource *root, struct resource *new)
210 {
211         struct resource *conflict;
212
213         write_lock(&resource_lock);
214         conflict = __request_resource(root, new);
215         write_unlock(&resource_lock);
216         return conflict;
217 }
218
219 EXPORT_SYMBOL(____request_resource);
220
221 int release_resource(struct resource *old)
222 {
223         int retval;
224
225         write_lock(&resource_lock);
226         retval = __release_resource(old);
227         write_unlock(&resource_lock);
228         return retval;
229 }
230
231 EXPORT_SYMBOL(release_resource);
232
233 #ifdef CONFIG_MEMORY_HOTPLUG
234 /*
235  * Finds the lowest memory reosurce exists within [res->start.res->end)
236  * the caller must specify res->start, res->end, res->flags.
237  * If found, returns 0, res is overwritten, if not found, returns -1.
238  */
239 int find_next_system_ram(struct resource *res)
240 {
241         resource_size_t start, end;
242         struct resource *p;
243
244         BUG_ON(!res);
245
246         start = res->start;
247         end = res->end;
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                 }
258                 if (p->start >= start)
259                         break;
260         }
261         read_unlock(&resource_lock);
262         if (!p)
263                 return -1;
264         /* copy data */
265         res->start = p->start;
266         res->end = p->end;
267         return 0;
268 }
269 #endif
270
271 /*
272  * Find empty slot in the resource tree given range and alignment.
273  */
274 static int find_resource(struct resource *root, struct resource *new,
275                          resource_size_t size, resource_size_t min,
276                          resource_size_t max, resource_size_t align,
277                          void (*alignf)(void *, struct resource *,
278                                         resource_size_t, resource_size_t),
279                          void *alignf_data)
280 {
281         struct resource *this = root->child;
282
283         new->start = root->start;
284         /*
285          * Skip past an allocated resource that starts at 0, since the assignment
286          * of this->start - 1 to new->end below would cause an underflow.
287          */
288         if (this && this->start == 0) {
289                 new->start = this->end + 1;
290                 this = this->sibling;
291         }
292         for(;;) {
293                 if (this)
294                         new->end = this->start - 1;
295                 else
296                         new->end = root->end;
297                 if (new->start < min)
298                         new->start = min;
299                 if (new->end > max)
300                         new->end = max;
301                 new->start = ALIGN(new->start, align);
302                 if (alignf)
303                         alignf(alignf_data, new, size, align);
304                 if (new->start < new->end && new->end - new->start >= size - 1) {
305                         new->end = new->start + size - 1;
306                         return 0;
307                 }
308                 if (!this)
309                         break;
310                 new->start = this->end + 1;
311                 this = this->sibling;
312         }
313         return -EBUSY;
314 }
315
316 /*
317  * Allocate empty slot in the resource tree given range and alignment.
318  */
319 int allocate_resource(struct resource *root, struct resource *new,
320                       resource_size_t size, resource_size_t min,
321                       resource_size_t max, resource_size_t align,
322                       void (*alignf)(void *, struct resource *,
323                                      resource_size_t, resource_size_t),
324                       void *alignf_data)
325 {
326         int err;
327
328         write_lock(&resource_lock);
329         err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
330         if (err >= 0 && __request_resource(root, new))
331                 err = -EBUSY;
332         write_unlock(&resource_lock);
333         return err;
334 }
335
336 EXPORT_SYMBOL(allocate_resource);
337
338 /**
339  * insert_resource - Inserts a resource in the resource tree
340  * @parent: parent of the new resource
341  * @new: new resource to insert
342  *
343  * Returns 0 on success, -EBUSY if the resource can't be inserted.
344  *
345  * This function is equivalent of request_resource when no conflict
346  * happens. If a conflict happens, and the conflicting resources
347  * entirely fit within the range of the new resource, then the new
348  * resource is inserted and the conflicting resources become childs of
349  * the new resource.  Otherwise the new resource becomes the child of
350  * the conflicting resource
351  */
352 int insert_resource(struct resource *parent, struct resource *new)
353 {
354         int result;
355         struct resource *first, *next;
356
357         write_lock(&resource_lock);
358  begin:
359         result = 0;
360         first = __request_resource(parent, new);
361         if (!first)
362                 goto out;
363
364         result = -EBUSY;
365         if (first == parent)
366                 goto out;
367
368         /* Resource fully contained by the clashing resource? Recurse into it */
369         if (first->start <= new->start && first->end >= new->end) {
370                 parent = first;
371                 goto begin;
372         }
373
374         for (next = first; ; next = next->sibling) {
375                 /* Partial overlap? Bad, and unfixable */
376                 if (next->start < new->start || next->end > new->end)
377                         goto out;
378                 if (!next->sibling)
379                         break;
380                 if (next->sibling->start > new->end)
381                         break;
382         }
383
384         result = 0;
385
386         new->parent = parent;
387         new->sibling = next->sibling;
388         new->child = first;
389
390         next->sibling = NULL;
391         for (next = first; next; next = next->sibling)
392                 next->parent = new;
393
394         if (parent->child == first) {
395                 parent->child = new;
396         } else {
397                 next = parent->child;
398                 while (next->sibling != first)
399                         next = next->sibling;
400                 next->sibling = new;
401         }
402
403  out:
404         write_unlock(&resource_lock);
405         return result;
406 }
407
408 EXPORT_SYMBOL(insert_resource);
409
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  */
415 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
416 {
417         struct resource *tmp, *parent = res->parent;
418         resource_size_t end = start + size - 1;
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
451 EXPORT_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  */
465 struct resource * __request_region(struct resource *parent,
466                                    resource_size_t start, resource_size_t n,
467                                    const char *name)
468 {
469         struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
470
471         if (res) {
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
501 EXPORT_SYMBOL(__request_region);
502
503 int __check_region(struct resource *parent, resource_size_t start,
504                         resource_size_t n)
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
517 EXPORT_SYMBOL(__check_region);
518
519 void __release_region(struct resource *parent, resource_size_t start,
520                         resource_size_t n)
521 {
522         struct resource **p;
523         resource_size_t 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 "
553                 "<%016llx-%016llx>\n", (unsigned long long)start,
554                 (unsigned long long)end);
555 }
556
557 EXPORT_SYMBOL(__release_region);
558
559 /*
560  * Called from init/main.c to reserve IO ports.
561  */
562 #define MAXRESERVE 4
563 static 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);