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