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