| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/errno.h> | 
 | 12 | #include <linux/ioport.h> | 
 | 13 | #include <linux/init.h> | 
 | 14 | #include <linux/slab.h> | 
 | 15 | #include <linux/spinlock.h> | 
 | 16 | #include <linux/fs.h> | 
 | 17 | #include <linux/proc_fs.h> | 
| Alan Cox | 8b6d043 | 2010-03-29 19:38:00 +0200 | [diff] [blame] | 18 | #include <linux/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/seq_file.h> | 
| Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 20 | #include <linux/device.h> | 
| Suresh Siddha | d68612b | 2008-10-28 11:45:42 -0700 | [diff] [blame] | 21 | #include <linux/pfn.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/io.h> | 
 | 23 |  | 
 | 24 |  | 
 | 25 | struct resource ioport_resource = { | 
 | 26 | 	.name	= "PCI IO", | 
| Greg Kroah-Hartman | 6550e07 | 2006-06-12 17:11:31 -0700 | [diff] [blame] | 27 | 	.start	= 0, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | 	.end	= IO_SPACE_LIMIT, | 
 | 29 | 	.flags	= IORESOURCE_IO, | 
 | 30 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | EXPORT_SYMBOL(ioport_resource); | 
 | 32 |  | 
 | 33 | struct resource iomem_resource = { | 
 | 34 | 	.name	= "PCI mem", | 
| Greg Kroah-Hartman | 6550e07 | 2006-06-12 17:11:31 -0700 | [diff] [blame] | 35 | 	.start	= 0, | 
 | 36 | 	.end	= -1, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | 	.flags	= IORESOURCE_MEM, | 
 | 38 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | EXPORT_SYMBOL(iomem_resource); | 
 | 40 |  | 
 | 41 | static DEFINE_RWLOCK(resource_lock); | 
 | 42 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static void *r_next(struct seq_file *m, void *v, loff_t *pos) | 
 | 44 | { | 
 | 45 | 	struct resource *p = v; | 
 | 46 | 	(*pos)++; | 
 | 47 | 	if (p->child) | 
 | 48 | 		return p->child; | 
 | 49 | 	while (!p->sibling && p->parent) | 
 | 50 | 		p = p->parent; | 
 | 51 | 	return p->sibling; | 
 | 52 | } | 
 | 53 |  | 
| Ingo Molnar | 13eb837 | 2008-09-26 10:10:12 +0200 | [diff] [blame] | 54 | #ifdef CONFIG_PROC_FS | 
 | 55 |  | 
 | 56 | enum { MAX_IORES_LEVEL = 5 }; | 
 | 57 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | static void *r_start(struct seq_file *m, loff_t *pos) | 
 | 59 | 	__acquires(resource_lock) | 
 | 60 | { | 
 | 61 | 	struct resource *p = m->private; | 
 | 62 | 	loff_t l = 0; | 
 | 63 | 	read_lock(&resource_lock); | 
 | 64 | 	for (p = p->child; p && l < *pos; p = r_next(m, p, &l)) | 
 | 65 | 		; | 
 | 66 | 	return p; | 
 | 67 | } | 
 | 68 |  | 
 | 69 | static void r_stop(struct seq_file *m, void *v) | 
 | 70 | 	__releases(resource_lock) | 
 | 71 | { | 
 | 72 | 	read_unlock(&resource_lock); | 
 | 73 | } | 
 | 74 |  | 
 | 75 | static int r_show(struct seq_file *m, void *v) | 
 | 76 | { | 
 | 77 | 	struct resource *root = m->private; | 
 | 78 | 	struct resource *r = v, *p; | 
 | 79 | 	int width = root->end < 0x10000 ? 4 : 8; | 
 | 80 | 	int depth; | 
 | 81 |  | 
 | 82 | 	for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent) | 
 | 83 | 		if (p->parent == root) | 
 | 84 | 			break; | 
| Greg Kroah-Hartman | 685143a | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 85 | 	seq_printf(m, "%*s%0*llx-%0*llx : %s\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | 			depth * 2, "", | 
| Greg Kroah-Hartman | 685143a | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 87 | 			width, (unsigned long long) r->start, | 
 | 88 | 			width, (unsigned long long) r->end, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | 			r->name ? r->name : "<BAD>"); | 
 | 90 | 	return 0; | 
 | 91 | } | 
 | 92 |  | 
| Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 93 | static const struct seq_operations resource_op = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | 	.start	= r_start, | 
 | 95 | 	.next	= r_next, | 
 | 96 | 	.stop	= r_stop, | 
 | 97 | 	.show	= r_show, | 
 | 98 | }; | 
 | 99 |  | 
 | 100 | static int ioports_open(struct inode *inode, struct file *file) | 
 | 101 | { | 
 | 102 | 	int res = seq_open(file, &resource_op); | 
 | 103 | 	if (!res) { | 
 | 104 | 		struct seq_file *m = file->private_data; | 
 | 105 | 		m->private = &ioport_resource; | 
 | 106 | 	} | 
 | 107 | 	return res; | 
 | 108 | } | 
 | 109 |  | 
 | 110 | static int iomem_open(struct inode *inode, struct file *file) | 
 | 111 | { | 
 | 112 | 	int res = seq_open(file, &resource_op); | 
 | 113 | 	if (!res) { | 
 | 114 | 		struct seq_file *m = file->private_data; | 
 | 115 | 		m->private = &iomem_resource; | 
 | 116 | 	} | 
 | 117 | 	return res; | 
 | 118 | } | 
 | 119 |  | 
| Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 120 | static const struct file_operations proc_ioports_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | 	.open		= ioports_open, | 
 | 122 | 	.read		= seq_read, | 
 | 123 | 	.llseek		= seq_lseek, | 
 | 124 | 	.release	= seq_release, | 
 | 125 | }; | 
 | 126 |  | 
| Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 127 | static const struct file_operations proc_iomem_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | 	.open		= iomem_open, | 
 | 129 | 	.read		= seq_read, | 
 | 130 | 	.llseek		= seq_lseek, | 
 | 131 | 	.release	= seq_release, | 
 | 132 | }; | 
 | 133 |  | 
 | 134 | static int __init ioresources_init(void) | 
 | 135 | { | 
| Denis V. Lunev | c33fff0 | 2008-04-29 01:02:31 -0700 | [diff] [blame] | 136 | 	proc_create("ioports", 0, NULL, &proc_ioports_operations); | 
 | 137 | 	proc_create("iomem", 0, NULL, &proc_iomem_operations); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | 	return 0; | 
 | 139 | } | 
 | 140 | __initcall(ioresources_init); | 
 | 141 |  | 
 | 142 | #endif /* CONFIG_PROC_FS */ | 
 | 143 |  | 
 | 144 | /* Return the conflict entry if you can't request it */ | 
 | 145 | static struct resource * __request_resource(struct resource *root, struct resource *new) | 
 | 146 | { | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 147 | 	resource_size_t start = new->start; | 
 | 148 | 	resource_size_t end = new->end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | 	struct resource *tmp, **p; | 
 | 150 |  | 
 | 151 | 	if (end < start) | 
 | 152 | 		return root; | 
 | 153 | 	if (start < root->start) | 
 | 154 | 		return root; | 
 | 155 | 	if (end > root->end) | 
 | 156 | 		return root; | 
 | 157 | 	p = &root->child; | 
 | 158 | 	for (;;) { | 
 | 159 | 		tmp = *p; | 
 | 160 | 		if (!tmp || tmp->start > end) { | 
 | 161 | 			new->sibling = tmp; | 
 | 162 | 			*p = new; | 
 | 163 | 			new->parent = root; | 
 | 164 | 			return NULL; | 
 | 165 | 		} | 
 | 166 | 		p = &tmp->sibling; | 
 | 167 | 		if (tmp->end < start) | 
 | 168 | 			continue; | 
 | 169 | 		return tmp; | 
 | 170 | 	} | 
 | 171 | } | 
 | 172 |  | 
 | 173 | static int __release_resource(struct resource *old) | 
 | 174 | { | 
 | 175 | 	struct resource *tmp, **p; | 
 | 176 |  | 
 | 177 | 	p = &old->parent->child; | 
 | 178 | 	for (;;) { | 
 | 179 | 		tmp = *p; | 
 | 180 | 		if (!tmp) | 
 | 181 | 			break; | 
 | 182 | 		if (tmp == old) { | 
 | 183 | 			*p = tmp->sibling; | 
 | 184 | 			old->parent = NULL; | 
 | 185 | 			return 0; | 
 | 186 | 		} | 
 | 187 | 		p = &tmp->sibling; | 
 | 188 | 	} | 
 | 189 | 	return -EINVAL; | 
 | 190 | } | 
 | 191 |  | 
| Yinghai Lu | 5eeec0e | 2009-12-22 15:02:22 -0800 | [diff] [blame] | 192 | static void __release_child_resources(struct resource *r) | 
 | 193 | { | 
 | 194 | 	struct resource *tmp, *p; | 
 | 195 | 	resource_size_t size; | 
 | 196 |  | 
 | 197 | 	p = r->child; | 
 | 198 | 	r->child = NULL; | 
 | 199 | 	while (p) { | 
 | 200 | 		tmp = p; | 
 | 201 | 		p = p->sibling; | 
 | 202 |  | 
 | 203 | 		tmp->parent = NULL; | 
 | 204 | 		tmp->sibling = NULL; | 
 | 205 | 		__release_child_resources(tmp); | 
 | 206 |  | 
 | 207 | 		printk(KERN_DEBUG "release child resource %pR\n", tmp); | 
 | 208 | 		/* need to restore size, and keep flags */ | 
 | 209 | 		size = resource_size(tmp); | 
 | 210 | 		tmp->start = 0; | 
 | 211 | 		tmp->end = size - 1; | 
 | 212 | 	} | 
 | 213 | } | 
 | 214 |  | 
 | 215 | void release_child_resources(struct resource *r) | 
 | 216 | { | 
 | 217 | 	write_lock(&resource_lock); | 
 | 218 | 	__release_child_resources(r); | 
 | 219 | 	write_unlock(&resource_lock); | 
 | 220 | } | 
 | 221 |  | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 222 | /** | 
| Bjorn Helgaas | 66f1207 | 2010-03-11 17:01:09 -0700 | [diff] [blame] | 223 |  * request_resource_conflict - request and reserve an I/O or memory resource | 
 | 224 |  * @root: root resource descriptor | 
 | 225 |  * @new: resource descriptor desired by caller | 
 | 226 |  * | 
 | 227 |  * Returns 0 for success, conflict resource on error. | 
 | 228 |  */ | 
 | 229 | struct resource *request_resource_conflict(struct resource *root, struct resource *new) | 
 | 230 | { | 
 | 231 | 	struct resource *conflict; | 
 | 232 |  | 
 | 233 | 	write_lock(&resource_lock); | 
 | 234 | 	conflict = __request_resource(root, new); | 
 | 235 | 	write_unlock(&resource_lock); | 
 | 236 | 	return conflict; | 
 | 237 | } | 
 | 238 |  | 
 | 239 | /** | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 240 |  * request_resource - request and reserve an I/O or memory resource | 
 | 241 |  * @root: root resource descriptor | 
 | 242 |  * @new: resource descriptor desired by caller | 
 | 243 |  * | 
 | 244 |  * Returns 0 for success, negative error code on error. | 
 | 245 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | int request_resource(struct resource *root, struct resource *new) | 
 | 247 | { | 
 | 248 | 	struct resource *conflict; | 
 | 249 |  | 
| Bjorn Helgaas | 66f1207 | 2010-03-11 17:01:09 -0700 | [diff] [blame] | 250 | 	conflict = request_resource_conflict(root, new); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | 	return conflict ? -EBUSY : 0; | 
 | 252 | } | 
 | 253 |  | 
 | 254 | EXPORT_SYMBOL(request_resource); | 
 | 255 |  | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 256 | /** | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 257 |  * release_resource - release a previously reserved resource | 
 | 258 |  * @old: resource pointer | 
 | 259 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | int release_resource(struct resource *old) | 
 | 261 | { | 
 | 262 | 	int retval; | 
 | 263 |  | 
 | 264 | 	write_lock(&resource_lock); | 
 | 265 | 	retval = __release_resource(old); | 
 | 266 | 	write_unlock(&resource_lock); | 
 | 267 | 	return retval; | 
 | 268 | } | 
 | 269 |  | 
 | 270 | EXPORT_SYMBOL(release_resource); | 
 | 271 |  | 
| KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 272 | #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY) | 
| KAMEZAWA Hiroyuki | 2842f11 | 2006-06-27 02:53:36 -0700 | [diff] [blame] | 273 | /* | 
 | 274 |  * Finds the lowest memory reosurce exists within [res->start.res->end) | 
| KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 275 |  * the caller must specify res->start, res->end, res->flags and "name". | 
| KAMEZAWA Hiroyuki | 2842f11 | 2006-06-27 02:53:36 -0700 | [diff] [blame] | 276 |  * If found, returns 0, res is overwritten, if not found, returns -1. | 
 | 277 |  */ | 
| KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 278 | static int find_next_system_ram(struct resource *res, char *name) | 
| KAMEZAWA Hiroyuki | 2842f11 | 2006-06-27 02:53:36 -0700 | [diff] [blame] | 279 | { | 
 | 280 | 	resource_size_t start, end; | 
 | 281 | 	struct resource *p; | 
 | 282 |  | 
 | 283 | 	BUG_ON(!res); | 
 | 284 |  | 
 | 285 | 	start = res->start; | 
 | 286 | 	end = res->end; | 
| KAMEZAWA Hiroyuki | 58c1b5b | 2006-08-05 12:15:01 -0700 | [diff] [blame] | 287 | 	BUG_ON(start >= end); | 
| KAMEZAWA Hiroyuki | 2842f11 | 2006-06-27 02:53:36 -0700 | [diff] [blame] | 288 |  | 
 | 289 | 	read_lock(&resource_lock); | 
 | 290 | 	for (p = iomem_resource.child; p ; p = p->sibling) { | 
 | 291 | 		/* system ram is just marked as IORESOURCE_MEM */ | 
 | 292 | 		if (p->flags != res->flags) | 
 | 293 | 			continue; | 
| KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 294 | 		if (name && strcmp(p->name, name)) | 
 | 295 | 			continue; | 
| KAMEZAWA Hiroyuki | 2842f11 | 2006-06-27 02:53:36 -0700 | [diff] [blame] | 296 | 		if (p->start > end) { | 
 | 297 | 			p = NULL; | 
 | 298 | 			break; | 
 | 299 | 		} | 
| KAMEZAWA Hiroyuki | 58c1b5b | 2006-08-05 12:15:01 -0700 | [diff] [blame] | 300 | 		if ((p->end >= start) && (p->start < end)) | 
| KAMEZAWA Hiroyuki | 2842f11 | 2006-06-27 02:53:36 -0700 | [diff] [blame] | 301 | 			break; | 
 | 302 | 	} | 
 | 303 | 	read_unlock(&resource_lock); | 
 | 304 | 	if (!p) | 
 | 305 | 		return -1; | 
 | 306 | 	/* copy data */ | 
| KAMEZAWA Hiroyuki | 0f04ab5 | 2006-08-05 12:14:59 -0700 | [diff] [blame] | 307 | 	if (res->start < p->start) | 
 | 308 | 		res->start = p->start; | 
 | 309 | 	if (res->end > p->end) | 
 | 310 | 		res->end = p->end; | 
| KAMEZAWA Hiroyuki | 2842f11 | 2006-06-27 02:53:36 -0700 | [diff] [blame] | 311 | 	return 0; | 
 | 312 | } | 
| KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 313 |  | 
 | 314 | /* | 
 | 315 |  * This function calls callback against all memory range of "System RAM" | 
 | 316 |  * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY. | 
 | 317 |  * Now, this function is only for "System RAM". | 
 | 318 |  */ | 
 | 319 | int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages, | 
 | 320 | 		void *arg, int (*func)(unsigned long, unsigned long, void *)) | 
| KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 321 | { | 
 | 322 | 	struct resource res; | 
| Wu Fengguang | 37b99dd | 2010-03-01 21:55:51 +0800 | [diff] [blame] | 323 | 	unsigned long pfn, end_pfn; | 
| KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 324 | 	u64 orig_end; | 
 | 325 | 	int ret = -1; | 
| KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 326 |  | 
| KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 327 | 	res.start = (u64) start_pfn << PAGE_SHIFT; | 
 | 328 | 	res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1; | 
| Yasunori Goto | 887c3cb | 2007-11-14 16:59:20 -0800 | [diff] [blame] | 329 | 	res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; | 
| KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 330 | 	orig_end = res.end; | 
| KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 331 | 	while ((res.start < res.end) && | 
 | 332 | 		(find_next_system_ram(&res, "System RAM") >= 0)) { | 
| Wu Fengguang | 37b99dd | 2010-03-01 21:55:51 +0800 | [diff] [blame] | 333 | 		pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT; | 
 | 334 | 		end_pfn = (res.end + 1) >> PAGE_SHIFT; | 
 | 335 | 		if (end_pfn > pfn) | 
| H. Peter Anvin | f414966 | 2010-03-02 11:21:09 -0800 | [diff] [blame] | 336 | 			ret = (*func)(pfn, end_pfn - pfn, arg); | 
| KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 337 | 		if (ret) | 
 | 338 | 			break; | 
 | 339 | 		res.start = res.end + 1; | 
 | 340 | 		res.end = orig_end; | 
 | 341 | 	} | 
 | 342 | 	return ret; | 
 | 343 | } | 
 | 344 |  | 
| KAMEZAWA Hiroyuki | 2842f11 | 2006-06-27 02:53:36 -0700 | [diff] [blame] | 345 | #endif | 
 | 346 |  | 
| Wu Fengguang | 61ef248 | 2010-01-22 16:16:19 +0800 | [diff] [blame] | 347 | static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg) | 
 | 348 | { | 
 | 349 | 	return 1; | 
 | 350 | } | 
 | 351 | /* | 
 | 352 |  * This generic page_is_ram() returns true if specified address is | 
 | 353 |  * registered as "System RAM" in iomem_resource list. | 
 | 354 |  */ | 
| Andrew Morton | e527300 | 2010-01-26 16:31:19 -0800 | [diff] [blame] | 355 | int __weak page_is_ram(unsigned long pfn) | 
| Wu Fengguang | 61ef248 | 2010-01-22 16:16:19 +0800 | [diff] [blame] | 356 | { | 
 | 357 | 	return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1; | 
 | 358 | } | 
 | 359 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | /* | 
 | 361 |  * Find empty slot in the resource tree given range and alignment. | 
 | 362 |  */ | 
 | 363 | static int find_resource(struct resource *root, struct resource *new, | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 364 | 			 resource_size_t size, resource_size_t min, | 
 | 365 | 			 resource_size_t max, resource_size_t align, | 
| Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 366 | 			 resource_size_t (*alignf)(void *, | 
| Dominik Brodowski | 3b7a17f | 2010-01-01 17:40:50 +0100 | [diff] [blame] | 367 | 						   const struct resource *, | 
| Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 368 | 						   resource_size_t, | 
 | 369 | 						   resource_size_t), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | 			 void *alignf_data) | 
 | 371 | { | 
 | 372 | 	struct resource *this = root->child; | 
| Dominik Brodowski | 0e2c8b8 | 2009-12-20 10:50:02 +0100 | [diff] [blame] | 373 | 	struct resource tmp = *new; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 |  | 
| Dominik Brodowski | 0e2c8b8 | 2009-12-20 10:50:02 +0100 | [diff] [blame] | 375 | 	tmp.start = root->start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | 	/* | 
 | 377 | 	 * Skip past an allocated resource that starts at 0, since the assignment | 
| Dominik Brodowski | 0e2c8b8 | 2009-12-20 10:50:02 +0100 | [diff] [blame] | 378 | 	 * of this->start - 1 to tmp->end below would cause an underflow. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | 	 */ | 
 | 380 | 	if (this && this->start == 0) { | 
| Dominik Brodowski | 0e2c8b8 | 2009-12-20 10:50:02 +0100 | [diff] [blame] | 381 | 		tmp.start = this->end + 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | 		this = this->sibling; | 
 | 383 | 	} | 
 | 384 | 	for(;;) { | 
 | 385 | 		if (this) | 
| Dominik Brodowski | 0e2c8b8 | 2009-12-20 10:50:02 +0100 | [diff] [blame] | 386 | 			tmp.end = this->start - 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | 		else | 
| Dominik Brodowski | 0e2c8b8 | 2009-12-20 10:50:02 +0100 | [diff] [blame] | 388 | 			tmp.end = root->end; | 
 | 389 | 		if (tmp.start < min) | 
 | 390 | 			tmp.start = min; | 
 | 391 | 		if (tmp.end > max) | 
 | 392 | 			tmp.end = max; | 
 | 393 | 		tmp.start = ALIGN(tmp.start, align); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | 		if (alignf) | 
| Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 395 | 			tmp.start = alignf(alignf_data, &tmp, size, align); | 
| Dominik Brodowski | 0e2c8b8 | 2009-12-20 10:50:02 +0100 | [diff] [blame] | 396 | 		if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) { | 
 | 397 | 			new->start = tmp.start; | 
 | 398 | 			new->end = tmp.start + size - 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | 			return 0; | 
 | 400 | 		} | 
 | 401 | 		if (!this) | 
 | 402 | 			break; | 
| Dominik Brodowski | 0e2c8b8 | 2009-12-20 10:50:02 +0100 | [diff] [blame] | 403 | 		tmp.start = this->end + 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | 		this = this->sibling; | 
 | 405 | 	} | 
 | 406 | 	return -EBUSY; | 
 | 407 | } | 
 | 408 |  | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 409 | /** | 
 | 410 |  * allocate_resource - allocate empty slot in the resource tree given range & alignment | 
 | 411 |  * @root: root resource descriptor | 
 | 412 |  * @new: resource descriptor desired by caller | 
 | 413 |  * @size: requested resource region size | 
 | 414 |  * @min: minimum size to allocate | 
 | 415 |  * @max: maximum size to allocate | 
 | 416 |  * @align: alignment requested, in bytes | 
 | 417 |  * @alignf: alignment function, optional, called if not NULL | 
 | 418 |  * @alignf_data: arbitrary data to pass to the @alignf function | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 |  */ | 
 | 420 | int allocate_resource(struct resource *root, struct resource *new, | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 421 | 		      resource_size_t size, resource_size_t min, | 
 | 422 | 		      resource_size_t max, resource_size_t align, | 
| Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 423 | 		      resource_size_t (*alignf)(void *, | 
| Dominik Brodowski | 3b7a17f | 2010-01-01 17:40:50 +0100 | [diff] [blame] | 424 | 						const struct resource *, | 
| Dominik Brodowski | b26b2d4 | 2010-01-01 17:40:49 +0100 | [diff] [blame] | 425 | 						resource_size_t, | 
 | 426 | 						resource_size_t), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | 		      void *alignf_data) | 
 | 428 | { | 
 | 429 | 	int err; | 
 | 430 |  | 
 | 431 | 	write_lock(&resource_lock); | 
 | 432 | 	err = find_resource(root, new, size, min, max, align, alignf, alignf_data); | 
 | 433 | 	if (err >= 0 && __request_resource(root, new)) | 
 | 434 | 		err = -EBUSY; | 
 | 435 | 	write_unlock(&resource_lock); | 
 | 436 | 	return err; | 
 | 437 | } | 
 | 438 |  | 
 | 439 | EXPORT_SYMBOL(allocate_resource); | 
 | 440 |  | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 441 | /* | 
 | 442 |  * Insert a resource into the resource tree. If successful, return NULL, | 
 | 443 |  * otherwise return the conflicting resource (compare to __request_resource()) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 |  */ | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 445 | static struct resource * __insert_resource(struct resource *parent, struct resource *new) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | 	struct resource *first, *next; | 
 | 448 |  | 
| Matthew Wilcox | d33b6fb | 2006-06-30 02:31:24 -0700 | [diff] [blame] | 449 | 	for (;; parent = first) { | 
| Matthew Wilcox | d33b6fb | 2006-06-30 02:31:24 -0700 | [diff] [blame] | 450 | 		first = __request_resource(parent, new); | 
 | 451 | 		if (!first) | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 452 | 			return first; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 |  | 
| Matthew Wilcox | d33b6fb | 2006-06-30 02:31:24 -0700 | [diff] [blame] | 454 | 		if (first == parent) | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 455 | 			return first; | 
| Matthew Wilcox | d33b6fb | 2006-06-30 02:31:24 -0700 | [diff] [blame] | 456 |  | 
 | 457 | 		if ((first->start > new->start) || (first->end < new->end)) | 
 | 458 | 			break; | 
 | 459 | 		if ((first->start == new->start) && (first->end == new->end)) | 
 | 460 | 			break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | 	} | 
 | 462 |  | 
 | 463 | 	for (next = first; ; next = next->sibling) { | 
 | 464 | 		/* Partial overlap? Bad, and unfixable */ | 
 | 465 | 		if (next->start < new->start || next->end > new->end) | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 466 | 			return next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | 		if (!next->sibling) | 
 | 468 | 			break; | 
 | 469 | 		if (next->sibling->start > new->end) | 
 | 470 | 			break; | 
 | 471 | 	} | 
 | 472 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | 	new->parent = parent; | 
 | 474 | 	new->sibling = next->sibling; | 
 | 475 | 	new->child = first; | 
 | 476 |  | 
 | 477 | 	next->sibling = NULL; | 
 | 478 | 	for (next = first; next; next = next->sibling) | 
 | 479 | 		next->parent = new; | 
 | 480 |  | 
 | 481 | 	if (parent->child == first) { | 
 | 482 | 		parent->child = new; | 
 | 483 | 	} else { | 
 | 484 | 		next = parent->child; | 
 | 485 | 		while (next->sibling != first) | 
 | 486 | 			next = next->sibling; | 
 | 487 | 		next->sibling = new; | 
 | 488 | 	} | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 489 | 	return NULL; | 
 | 490 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 |  | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 492 | /** | 
| Bjorn Helgaas | 66f1207 | 2010-03-11 17:01:09 -0700 | [diff] [blame] | 493 |  * insert_resource_conflict - Inserts resource in the resource tree | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 494 |  * @parent: parent of the new resource | 
 | 495 |  * @new: new resource to insert | 
 | 496 |  * | 
| Bjorn Helgaas | 66f1207 | 2010-03-11 17:01:09 -0700 | [diff] [blame] | 497 |  * Returns 0 on success, conflict resource if the resource can't be inserted. | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 498 |  * | 
| Bjorn Helgaas | 66f1207 | 2010-03-11 17:01:09 -0700 | [diff] [blame] | 499 |  * This function is equivalent to request_resource_conflict when no conflict | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 500 |  * happens. If a conflict happens, and the conflicting resources | 
 | 501 |  * entirely fit within the range of the new resource, then the new | 
 | 502 |  * resource is inserted and the conflicting resources become children of | 
 | 503 |  * the new resource. | 
 | 504 |  */ | 
| Bjorn Helgaas | 66f1207 | 2010-03-11 17:01:09 -0700 | [diff] [blame] | 505 | struct resource *insert_resource_conflict(struct resource *parent, struct resource *new) | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 506 | { | 
 | 507 | 	struct resource *conflict; | 
 | 508 |  | 
 | 509 | 	write_lock(&resource_lock); | 
 | 510 | 	conflict = __insert_resource(parent, new); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | 	write_unlock(&resource_lock); | 
| Bjorn Helgaas | 66f1207 | 2010-03-11 17:01:09 -0700 | [diff] [blame] | 512 | 	return conflict; | 
 | 513 | } | 
 | 514 |  | 
 | 515 | /** | 
 | 516 |  * insert_resource - Inserts a resource in the resource tree | 
 | 517 |  * @parent: parent of the new resource | 
 | 518 |  * @new: new resource to insert | 
 | 519 |  * | 
 | 520 |  * Returns 0 on success, -EBUSY if the resource can't be inserted. | 
 | 521 |  */ | 
 | 522 | int insert_resource(struct resource *parent, struct resource *new) | 
 | 523 | { | 
 | 524 | 	struct resource *conflict; | 
 | 525 |  | 
 | 526 | 	conflict = insert_resource_conflict(parent, new); | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 527 | 	return conflict ? -EBUSY : 0; | 
 | 528 | } | 
 | 529 |  | 
 | 530 | /** | 
 | 531 |  * insert_resource_expand_to_fit - Insert a resource into the resource tree | 
| Randy Dunlap | 6781f4a | 2008-08-31 20:31:55 -0700 | [diff] [blame] | 532 |  * @root: root resource descriptor | 
| Linus Torvalds | bef69ea | 2008-08-29 20:18:31 -0700 | [diff] [blame] | 533 |  * @new: new resource to insert | 
 | 534 |  * | 
 | 535 |  * Insert a resource into the resource tree, possibly expanding it in order | 
 | 536 |  * to make it encompass any conflicting resources. | 
 | 537 |  */ | 
 | 538 | void insert_resource_expand_to_fit(struct resource *root, struct resource *new) | 
 | 539 | { | 
 | 540 | 	if (new->parent) | 
 | 541 | 		return; | 
 | 542 |  | 
 | 543 | 	write_lock(&resource_lock); | 
 | 544 | 	for (;;) { | 
 | 545 | 		struct resource *conflict; | 
 | 546 |  | 
 | 547 | 		conflict = __insert_resource(root, new); | 
 | 548 | 		if (!conflict) | 
 | 549 | 			break; | 
 | 550 | 		if (conflict == root) | 
 | 551 | 			break; | 
 | 552 |  | 
 | 553 | 		/* Ok, expand resource to cover the conflict, then try again .. */ | 
 | 554 | 		if (conflict->start < new->start) | 
 | 555 | 			new->start = conflict->start; | 
 | 556 | 		if (conflict->end > new->end) | 
 | 557 | 			new->end = conflict->end; | 
 | 558 |  | 
 | 559 | 		printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name); | 
 | 560 | 	} | 
 | 561 | 	write_unlock(&resource_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } | 
 | 563 |  | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 564 | /** | 
 | 565 |  * adjust_resource - modify a resource's start and size | 
 | 566 |  * @res: resource to modify | 
 | 567 |  * @start: new start value | 
 | 568 |  * @size: new size | 
 | 569 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 |  * Given an existing resource, change its start and size to match the | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 571 |  * arguments.  Returns 0 on success, -EBUSY if it can't fit. | 
 | 572 |  * Existing children of the resource are assumed to be immutable. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 |  */ | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 574 | int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { | 
 | 576 | 	struct resource *tmp, *parent = res->parent; | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 577 | 	resource_size_t end = start + size - 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | 	int result = -EBUSY; | 
 | 579 |  | 
 | 580 | 	write_lock(&resource_lock); | 
 | 581 |  | 
 | 582 | 	if ((start < parent->start) || (end > parent->end)) | 
 | 583 | 		goto out; | 
 | 584 |  | 
 | 585 | 	for (tmp = res->child; tmp; tmp = tmp->sibling) { | 
 | 586 | 		if ((tmp->start < start) || (tmp->end > end)) | 
 | 587 | 			goto out; | 
 | 588 | 	} | 
 | 589 |  | 
 | 590 | 	if (res->sibling && (res->sibling->start <= end)) | 
 | 591 | 		goto out; | 
 | 592 |  | 
 | 593 | 	tmp = parent->child; | 
 | 594 | 	if (tmp != res) { | 
 | 595 | 		while (tmp->sibling != res) | 
 | 596 | 			tmp = tmp->sibling; | 
 | 597 | 		if (start <= tmp->end) | 
 | 598 | 			goto out; | 
 | 599 | 	} | 
 | 600 |  | 
 | 601 | 	res->start = start; | 
 | 602 | 	res->end = end; | 
 | 603 | 	result = 0; | 
 | 604 |  | 
 | 605 |  out: | 
 | 606 | 	write_unlock(&resource_lock); | 
 | 607 | 	return result; | 
 | 608 | } | 
 | 609 |  | 
| Yinghai Lu | 268364a | 2008-09-04 21:02:44 +0200 | [diff] [blame] | 610 | static void __init __reserve_region_with_split(struct resource *root, | 
 | 611 | 		resource_size_t start, resource_size_t end, | 
 | 612 | 		const char *name) | 
 | 613 | { | 
 | 614 | 	struct resource *parent = root; | 
 | 615 | 	struct resource *conflict; | 
| Linus Torvalds | 42c0202 | 2008-11-01 09:53:58 -0700 | [diff] [blame] | 616 | 	struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC); | 
| Yinghai Lu | 268364a | 2008-09-04 21:02:44 +0200 | [diff] [blame] | 617 |  | 
 | 618 | 	if (!res) | 
 | 619 | 		return; | 
 | 620 |  | 
 | 621 | 	res->name = name; | 
 | 622 | 	res->start = start; | 
 | 623 | 	res->end = end; | 
 | 624 | 	res->flags = IORESOURCE_BUSY; | 
 | 625 |  | 
| Linus Torvalds | ff54250 | 2009-04-18 21:44:24 -0700 | [diff] [blame] | 626 | 	conflict = __request_resource(parent, res); | 
 | 627 | 	if (!conflict) | 
 | 628 | 		return; | 
| Yinghai Lu | 268364a | 2008-09-04 21:02:44 +0200 | [diff] [blame] | 629 |  | 
| Linus Torvalds | ff54250 | 2009-04-18 21:44:24 -0700 | [diff] [blame] | 630 | 	/* failed, split and try again */ | 
 | 631 | 	kfree(res); | 
| Yinghai Lu | 268364a | 2008-09-04 21:02:44 +0200 | [diff] [blame] | 632 |  | 
| Linus Torvalds | ff54250 | 2009-04-18 21:44:24 -0700 | [diff] [blame] | 633 | 	/* conflict covered whole area */ | 
 | 634 | 	if (conflict->start <= start && conflict->end >= end) | 
 | 635 | 		return; | 
| Yinghai Lu | 268364a | 2008-09-04 21:02:44 +0200 | [diff] [blame] | 636 |  | 
| Linus Torvalds | ff54250 | 2009-04-18 21:44:24 -0700 | [diff] [blame] | 637 | 	if (conflict->start > start) | 
 | 638 | 		__reserve_region_with_split(root, start, conflict->start-1, name); | 
 | 639 | 	if (conflict->end < end) | 
 | 640 | 		__reserve_region_with_split(root, conflict->end+1, end, name); | 
| Yinghai Lu | 268364a | 2008-09-04 21:02:44 +0200 | [diff] [blame] | 641 | } | 
 | 642 |  | 
| Paul Mundt | bea9211 | 2008-10-22 19:31:11 +0900 | [diff] [blame] | 643 | void __init reserve_region_with_split(struct resource *root, | 
| Yinghai Lu | 268364a | 2008-09-04 21:02:44 +0200 | [diff] [blame] | 644 | 		resource_size_t start, resource_size_t end, | 
 | 645 | 		const char *name) | 
 | 646 | { | 
 | 647 | 	write_lock(&resource_lock); | 
 | 648 | 	__reserve_region_with_split(root, start, end, name); | 
 | 649 | 	write_unlock(&resource_lock); | 
 | 650 | } | 
 | 651 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | EXPORT_SYMBOL(adjust_resource); | 
 | 653 |  | 
| Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 654 | /** | 
 | 655 |  * resource_alignment - calculate resource's alignment | 
 | 656 |  * @res: resource pointer | 
 | 657 |  * | 
 | 658 |  * Returns alignment on success, 0 (invalid alignment) on failure. | 
 | 659 |  */ | 
 | 660 | resource_size_t resource_alignment(struct resource *res) | 
 | 661 | { | 
 | 662 | 	switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) { | 
 | 663 | 	case IORESOURCE_SIZEALIGN: | 
| Magnus Damm | 1a4e564 | 2008-07-29 22:32:57 -0700 | [diff] [blame] | 664 | 		return resource_size(res); | 
| Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 665 | 	case IORESOURCE_STARTALIGN: | 
 | 666 | 		return res->start; | 
 | 667 | 	default: | 
 | 668 | 		return 0; | 
 | 669 | 	} | 
 | 670 | } | 
 | 671 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | /* | 
 | 673 |  * This is compatibility stuff for IO resources. | 
 | 674 |  * | 
 | 675 |  * Note how this, unlike the above, knows about | 
 | 676 |  * the IO flag meanings (busy etc). | 
 | 677 |  * | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 678 |  * request_region creates a new busy region. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 |  * | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 680 |  * check_region returns non-zero if the area is already busy. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 |  * | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 682 |  * release_region releases a matching busy region. | 
 | 683 |  */ | 
 | 684 |  | 
| Alan Cox | 8b6d043 | 2010-03-29 19:38:00 +0200 | [diff] [blame] | 685 | static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait); | 
 | 686 |  | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 687 | /** | 
 | 688 |  * __request_region - create a new busy resource region | 
 | 689 |  * @parent: parent resource descriptor | 
 | 690 |  * @start: resource start address | 
 | 691 |  * @n: resource region size | 
 | 692 |  * @name: reserving caller's ID string | 
| Randy Dunlap | 6ae301e | 2009-01-15 13:51:01 -0800 | [diff] [blame] | 693 |  * @flags: IO resource flags | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 |  */ | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 695 | struct resource * __request_region(struct resource *parent, | 
 | 696 | 				   resource_size_t start, resource_size_t n, | 
| Arjan van de Ven | e8de148 | 2008-10-22 19:55:31 -0700 | [diff] [blame] | 697 | 				   const char *name, int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | { | 
| Alan Cox | 8b6d043 | 2010-03-29 19:38:00 +0200 | [diff] [blame] | 699 | 	DECLARE_WAITQUEUE(wait, current); | 
| Pekka J Enberg | dd39271 | 2005-09-06 15:18:31 -0700 | [diff] [blame] | 700 | 	struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 |  | 
| Bjorn Helgaas | c26ec88 | 2008-10-15 22:05:14 -0700 | [diff] [blame] | 702 | 	if (!res) | 
 | 703 | 		return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 |  | 
| Bjorn Helgaas | c26ec88 | 2008-10-15 22:05:14 -0700 | [diff] [blame] | 705 | 	res->name = name; | 
 | 706 | 	res->start = start; | 
 | 707 | 	res->end = start + n - 1; | 
 | 708 | 	res->flags = IORESOURCE_BUSY; | 
| Arjan van de Ven | e8de148 | 2008-10-22 19:55:31 -0700 | [diff] [blame] | 709 | 	res->flags |= flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 |  | 
| Bjorn Helgaas | c26ec88 | 2008-10-15 22:05:14 -0700 | [diff] [blame] | 711 | 	write_lock(&resource_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 |  | 
| Bjorn Helgaas | c26ec88 | 2008-10-15 22:05:14 -0700 | [diff] [blame] | 713 | 	for (;;) { | 
 | 714 | 		struct resource *conflict; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 |  | 
| Bjorn Helgaas | c26ec88 | 2008-10-15 22:05:14 -0700 | [diff] [blame] | 716 | 		conflict = __request_resource(parent, res); | 
 | 717 | 		if (!conflict) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | 			break; | 
| Bjorn Helgaas | c26ec88 | 2008-10-15 22:05:14 -0700 | [diff] [blame] | 719 | 		if (conflict != parent) { | 
 | 720 | 			parent = conflict; | 
 | 721 | 			if (!(conflict->flags & IORESOURCE_BUSY)) | 
 | 722 | 				continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | 		} | 
| Alan Cox | 8b6d043 | 2010-03-29 19:38:00 +0200 | [diff] [blame] | 724 | 		if (conflict->flags & flags & IORESOURCE_MUXED) { | 
 | 725 | 			add_wait_queue(&muxed_resource_wait, &wait); | 
 | 726 | 			write_unlock(&resource_lock); | 
 | 727 | 			set_current_state(TASK_UNINTERRUPTIBLE); | 
 | 728 | 			schedule(); | 
 | 729 | 			remove_wait_queue(&muxed_resource_wait, &wait); | 
 | 730 | 			write_lock(&resource_lock); | 
 | 731 | 			continue; | 
 | 732 | 		} | 
| Bjorn Helgaas | c26ec88 | 2008-10-15 22:05:14 -0700 | [diff] [blame] | 733 | 		/* Uhhuh, that didn't work out.. */ | 
 | 734 | 		kfree(res); | 
 | 735 | 		res = NULL; | 
 | 736 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | 	} | 
| Bjorn Helgaas | c26ec88 | 2008-10-15 22:05:14 -0700 | [diff] [blame] | 738 | 	write_unlock(&resource_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | 	return res; | 
 | 740 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | EXPORT_SYMBOL(__request_region); | 
 | 742 |  | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 743 | /** | 
 | 744 |  * __check_region - check if a resource region is busy or free | 
 | 745 |  * @parent: parent resource descriptor | 
 | 746 |  * @start: resource start address | 
 | 747 |  * @n: resource region size | 
 | 748 |  * | 
 | 749 |  * Returns 0 if the region is free at the moment it is checked, | 
 | 750 |  * returns %-EBUSY if the region is busy. | 
 | 751 |  * | 
 | 752 |  * NOTE: | 
 | 753 |  * This function is deprecated because its use is racy. | 
 | 754 |  * Even if it returns 0, a subsequent call to request_region() | 
 | 755 |  * may fail because another driver etc. just allocated the region. | 
 | 756 |  * Do NOT use it.  It will be removed from the kernel. | 
 | 757 |  */ | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 758 | int __check_region(struct resource *parent, resource_size_t start, | 
 | 759 | 			resource_size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | { | 
 | 761 | 	struct resource * res; | 
 | 762 |  | 
| Arjan van de Ven | e8de148 | 2008-10-22 19:55:31 -0700 | [diff] [blame] | 763 | 	res = __request_region(parent, start, n, "check-region", 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | 	if (!res) | 
 | 765 | 		return -EBUSY; | 
 | 766 |  | 
 | 767 | 	release_resource(res); | 
 | 768 | 	kfree(res); | 
 | 769 | 	return 0; | 
 | 770 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | EXPORT_SYMBOL(__check_region); | 
 | 772 |  | 
| Randy Dunlap | e1ca66d | 2006-10-03 01:13:51 -0700 | [diff] [blame] | 773 | /** | 
 | 774 |  * __release_region - release a previously reserved resource region | 
 | 775 |  * @parent: parent resource descriptor | 
 | 776 |  * @start: resource start address | 
 | 777 |  * @n: resource region size | 
 | 778 |  * | 
 | 779 |  * The described resource region must match a currently busy region. | 
 | 780 |  */ | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 781 | void __release_region(struct resource *parent, resource_size_t start, | 
 | 782 | 			resource_size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { | 
 | 784 | 	struct resource **p; | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 785 | 	resource_size_t end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 |  | 
 | 787 | 	p = &parent->child; | 
 | 788 | 	end = start + n - 1; | 
 | 789 |  | 
 | 790 | 	write_lock(&resource_lock); | 
 | 791 |  | 
 | 792 | 	for (;;) { | 
 | 793 | 		struct resource *res = *p; | 
 | 794 |  | 
 | 795 | 		if (!res) | 
 | 796 | 			break; | 
 | 797 | 		if (res->start <= start && res->end >= end) { | 
 | 798 | 			if (!(res->flags & IORESOURCE_BUSY)) { | 
 | 799 | 				p = &res->child; | 
 | 800 | 				continue; | 
 | 801 | 			} | 
 | 802 | 			if (res->start != start || res->end != end) | 
 | 803 | 				break; | 
 | 804 | 			*p = res->sibling; | 
 | 805 | 			write_unlock(&resource_lock); | 
| Alan Cox | 8b6d043 | 2010-03-29 19:38:00 +0200 | [diff] [blame] | 806 | 			if (res->flags & IORESOURCE_MUXED) | 
 | 807 | 				wake_up(&muxed_resource_wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | 			kfree(res); | 
 | 809 | 			return; | 
 | 810 | 		} | 
 | 811 | 		p = &res->sibling; | 
 | 812 | 	} | 
 | 813 |  | 
 | 814 | 	write_unlock(&resource_lock); | 
 | 815 |  | 
| Greg Kroah-Hartman | 685143a | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 816 | 	printk(KERN_WARNING "Trying to free nonexistent resource " | 
 | 817 | 		"<%016llx-%016llx>\n", (unsigned long long)start, | 
 | 818 | 		(unsigned long long)end); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | EXPORT_SYMBOL(__release_region); | 
 | 821 |  | 
 | 822 | /* | 
| Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 823 |  * Managed region resource | 
 | 824 |  */ | 
 | 825 | struct region_devres { | 
 | 826 | 	struct resource *parent; | 
 | 827 | 	resource_size_t start; | 
 | 828 | 	resource_size_t n; | 
 | 829 | }; | 
 | 830 |  | 
 | 831 | static void devm_region_release(struct device *dev, void *res) | 
 | 832 | { | 
 | 833 | 	struct region_devres *this = res; | 
 | 834 |  | 
 | 835 | 	__release_region(this->parent, this->start, this->n); | 
 | 836 | } | 
 | 837 |  | 
 | 838 | static int devm_region_match(struct device *dev, void *res, void *match_data) | 
 | 839 | { | 
 | 840 | 	struct region_devres *this = res, *match = match_data; | 
 | 841 |  | 
 | 842 | 	return this->parent == match->parent && | 
 | 843 | 		this->start == match->start && this->n == match->n; | 
 | 844 | } | 
 | 845 |  | 
 | 846 | struct resource * __devm_request_region(struct device *dev, | 
 | 847 | 				struct resource *parent, resource_size_t start, | 
 | 848 | 				resource_size_t n, const char *name) | 
 | 849 | { | 
 | 850 | 	struct region_devres *dr = NULL; | 
 | 851 | 	struct resource *res; | 
 | 852 |  | 
 | 853 | 	dr = devres_alloc(devm_region_release, sizeof(struct region_devres), | 
 | 854 | 			  GFP_KERNEL); | 
 | 855 | 	if (!dr) | 
 | 856 | 		return NULL; | 
 | 857 |  | 
 | 858 | 	dr->parent = parent; | 
 | 859 | 	dr->start = start; | 
 | 860 | 	dr->n = n; | 
 | 861 |  | 
| Arjan van de Ven | e8de148 | 2008-10-22 19:55:31 -0700 | [diff] [blame] | 862 | 	res = __request_region(parent, start, n, name, 0); | 
| Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 863 | 	if (res) | 
 | 864 | 		devres_add(dev, dr); | 
 | 865 | 	else | 
 | 866 | 		devres_free(dr); | 
 | 867 |  | 
 | 868 | 	return res; | 
 | 869 | } | 
 | 870 | EXPORT_SYMBOL(__devm_request_region); | 
 | 871 |  | 
 | 872 | void __devm_release_region(struct device *dev, struct resource *parent, | 
 | 873 | 			   resource_size_t start, resource_size_t n) | 
 | 874 | { | 
 | 875 | 	struct region_devres match_data = { parent, start, n }; | 
 | 876 |  | 
 | 877 | 	__release_region(parent, start, n); | 
 | 878 | 	WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match, | 
 | 879 | 			       &match_data)); | 
 | 880 | } | 
 | 881 | EXPORT_SYMBOL(__devm_release_region); | 
 | 882 |  | 
 | 883 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 |  * Called from init/main.c to reserve IO ports. | 
 | 885 |  */ | 
 | 886 | #define MAXRESERVE 4 | 
 | 887 | static int __init reserve_setup(char *str) | 
 | 888 | { | 
 | 889 | 	static int reserved; | 
 | 890 | 	static struct resource reserve[MAXRESERVE]; | 
 | 891 |  | 
 | 892 | 	for (;;) { | 
| Zhang Rui | 8bc1ad7 | 2009-06-30 11:41:31 -0700 | [diff] [blame] | 893 | 		unsigned int io_start, io_num; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | 		int x = reserved; | 
 | 895 |  | 
 | 896 | 		if (get_option (&str, &io_start) != 2) | 
 | 897 | 			break; | 
 | 898 | 		if (get_option (&str, &io_num)   == 0) | 
 | 899 | 			break; | 
 | 900 | 		if (x < MAXRESERVE) { | 
 | 901 | 			struct resource *res = reserve + x; | 
 | 902 | 			res->name = "reserved"; | 
 | 903 | 			res->start = io_start; | 
 | 904 | 			res->end = io_start + io_num - 1; | 
 | 905 | 			res->flags = IORESOURCE_BUSY; | 
 | 906 | 			res->child = NULL; | 
 | 907 | 			if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0) | 
 | 908 | 				reserved = x+1; | 
 | 909 | 		} | 
 | 910 | 	} | 
 | 911 | 	return 1; | 
 | 912 | } | 
 | 913 |  | 
 | 914 | __setup("reserve=", reserve_setup); | 
| Suresh Siddha | 379daf6 | 2008-09-25 18:43:34 -0700 | [diff] [blame] | 915 |  | 
 | 916 | /* | 
 | 917 |  * Check if the requested addr and size spans more than any slot in the | 
 | 918 |  * iomem resource tree. | 
 | 919 |  */ | 
 | 920 | int iomem_map_sanity_check(resource_size_t addr, unsigned long size) | 
 | 921 | { | 
 | 922 | 	struct resource *p = &iomem_resource; | 
 | 923 | 	int err = 0; | 
 | 924 | 	loff_t l; | 
 | 925 |  | 
 | 926 | 	read_lock(&resource_lock); | 
 | 927 | 	for (p = p->child; p ; p = r_next(NULL, p, &l)) { | 
 | 928 | 		/* | 
 | 929 | 		 * We can probably skip the resources without | 
 | 930 | 		 * IORESOURCE_IO attribute? | 
 | 931 | 		 */ | 
 | 932 | 		if (p->start >= addr + size) | 
 | 933 | 			continue; | 
 | 934 | 		if (p->end < addr) | 
 | 935 | 			continue; | 
| Suresh Siddha | d68612b | 2008-10-28 11:45:42 -0700 | [diff] [blame] | 936 | 		if (PFN_DOWN(p->start) <= PFN_DOWN(addr) && | 
 | 937 | 		    PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1)) | 
| Suresh Siddha | 379daf6 | 2008-09-25 18:43:34 -0700 | [diff] [blame] | 938 | 			continue; | 
| Arjan van de Ven | 3ac5266 | 2008-12-13 09:15:27 -0800 | [diff] [blame] | 939 | 		/* | 
 | 940 | 		 * if a resource is "BUSY", it's not a hardware resource | 
 | 941 | 		 * but a driver mapping of such a resource; we don't want | 
 | 942 | 		 * to warn for those; some drivers legitimately map only | 
 | 943 | 		 * partial hardware resources. (example: vesafb) | 
 | 944 | 		 */ | 
 | 945 | 		if (p->flags & IORESOURCE_BUSY) | 
 | 946 | 			continue; | 
 | 947 |  | 
| Suresh Siddha | 379daf6 | 2008-09-25 18:43:34 -0700 | [diff] [blame] | 948 | 		printk(KERN_WARNING "resource map sanity check conflict: " | 
 | 949 | 		       "0x%llx 0x%llx 0x%llx 0x%llx %s\n", | 
| Ingo Molnar | 13eb837 | 2008-09-26 10:10:12 +0200 | [diff] [blame] | 950 | 		       (unsigned long long)addr, | 
 | 951 | 		       (unsigned long long)(addr + size - 1), | 
 | 952 | 		       (unsigned long long)p->start, | 
 | 953 | 		       (unsigned long long)p->end, | 
 | 954 | 		       p->name); | 
| Suresh Siddha | 379daf6 | 2008-09-25 18:43:34 -0700 | [diff] [blame] | 955 | 		err = -1; | 
 | 956 | 		break; | 
 | 957 | 	} | 
 | 958 | 	read_unlock(&resource_lock); | 
 | 959 |  | 
 | 960 | 	return err; | 
 | 961 | } | 
| Arjan van de Ven | e8de148 | 2008-10-22 19:55:31 -0700 | [diff] [blame] | 962 |  | 
 | 963 | #ifdef CONFIG_STRICT_DEVMEM | 
 | 964 | static int strict_iomem_checks = 1; | 
 | 965 | #else | 
 | 966 | static int strict_iomem_checks; | 
 | 967 | #endif | 
 | 968 |  | 
 | 969 | /* | 
 | 970 |  * check if an address is reserved in the iomem resource tree | 
 | 971 |  * returns 1 if reserved, 0 if not reserved. | 
 | 972 |  */ | 
 | 973 | int iomem_is_exclusive(u64 addr) | 
 | 974 | { | 
 | 975 | 	struct resource *p = &iomem_resource; | 
 | 976 | 	int err = 0; | 
 | 977 | 	loff_t l; | 
 | 978 | 	int size = PAGE_SIZE; | 
 | 979 |  | 
 | 980 | 	if (!strict_iomem_checks) | 
 | 981 | 		return 0; | 
 | 982 |  | 
 | 983 | 	addr = addr & PAGE_MASK; | 
 | 984 |  | 
 | 985 | 	read_lock(&resource_lock); | 
 | 986 | 	for (p = p->child; p ; p = r_next(NULL, p, &l)) { | 
 | 987 | 		/* | 
 | 988 | 		 * We can probably skip the resources without | 
 | 989 | 		 * IORESOURCE_IO attribute? | 
 | 990 | 		 */ | 
 | 991 | 		if (p->start >= addr + size) | 
 | 992 | 			break; | 
 | 993 | 		if (p->end < addr) | 
 | 994 | 			continue; | 
 | 995 | 		if (p->flags & IORESOURCE_BUSY && | 
 | 996 | 		     p->flags & IORESOURCE_EXCLUSIVE) { | 
 | 997 | 			err = 1; | 
 | 998 | 			break; | 
 | 999 | 		} | 
 | 1000 | 	} | 
 | 1001 | 	read_unlock(&resource_lock); | 
 | 1002 |  | 
 | 1003 | 	return err; | 
 | 1004 | } | 
 | 1005 |  | 
 | 1006 | static int __init strict_iomem(char *str) | 
 | 1007 | { | 
 | 1008 | 	if (strstr(str, "relaxed")) | 
 | 1009 | 		strict_iomem_checks = 0; | 
 | 1010 | 	if (strstr(str, "strict")) | 
 | 1011 | 		strict_iomem_checks = 1; | 
 | 1012 | 	return 1; | 
 | 1013 | } | 
 | 1014 |  | 
 | 1015 | __setup("iomem=", strict_iomem); |