diff options
-rw-r--r-- | include/linux/ioport.h | 6 | ||||
-rw-r--r-- | kernel/resource.c | 101 |
2 files changed, 98 insertions, 9 deletions
diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 5e3a906cc089..142ec544167c 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h | |||
@@ -237,6 +237,12 @@ extern int iomem_is_exclusive(u64 addr); | |||
237 | extern int | 237 | extern int |
238 | walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages, | 238 | walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages, |
239 | void *arg, int (*func)(unsigned long, unsigned long, void *)); | 239 | void *arg, int (*func)(unsigned long, unsigned long, void *)); |
240 | extern int | ||
241 | walk_system_ram_res(u64 start, u64 end, void *arg, | ||
242 | int (*func)(u64, u64, void *)); | ||
243 | extern int | ||
244 | walk_iomem_res(char *name, unsigned long flags, u64 start, u64 end, void *arg, | ||
245 | int (*func)(u64, u64, void *)); | ||
240 | 246 | ||
241 | /* True if any part of r1 overlaps r2 */ | 247 | /* True if any part of r1 overlaps r2 */ |
242 | static inline bool resource_overlaps(struct resource *r1, struct resource *r2) | 248 | static inline bool resource_overlaps(struct resource *r1, struct resource *r2) |
diff --git a/kernel/resource.c b/kernel/resource.c index 3c2237ac32db..da14b8d09296 100644 --- a/kernel/resource.c +++ b/kernel/resource.c | |||
@@ -59,10 +59,12 @@ static DEFINE_RWLOCK(resource_lock); | |||
59 | static struct resource *bootmem_resource_free; | 59 | static struct resource *bootmem_resource_free; |
60 | static DEFINE_SPINLOCK(bootmem_resource_lock); | 60 | static DEFINE_SPINLOCK(bootmem_resource_lock); |
61 | 61 | ||
62 | static void *r_next(struct seq_file *m, void *v, loff_t *pos) | 62 | static struct resource *next_resource(struct resource *p, bool sibling_only) |
63 | { | 63 | { |
64 | struct resource *p = v; | 64 | /* Caller wants to traverse through siblings only */ |
65 | (*pos)++; | 65 | if (sibling_only) |
66 | return p->sibling; | ||
67 | |||
66 | if (p->child) | 68 | if (p->child) |
67 | return p->child; | 69 | return p->child; |
68 | while (!p->sibling && p->parent) | 70 | while (!p->sibling && p->parent) |
@@ -70,6 +72,13 @@ static void *r_next(struct seq_file *m, void *v, loff_t *pos) | |||
70 | return p->sibling; | 72 | return p->sibling; |
71 | } | 73 | } |
72 | 74 | ||
75 | static void *r_next(struct seq_file *m, void *v, loff_t *pos) | ||
76 | { | ||
77 | struct resource *p = v; | ||
78 | (*pos)++; | ||
79 | return (void *)next_resource(p, false); | ||
80 | } | ||
81 | |||
73 | #ifdef CONFIG_PROC_FS | 82 | #ifdef CONFIG_PROC_FS |
74 | 83 | ||
75 | enum { MAX_IORES_LEVEL = 5 }; | 84 | enum { MAX_IORES_LEVEL = 5 }; |
@@ -322,16 +331,19 @@ int release_resource(struct resource *old) | |||
322 | 331 | ||
323 | EXPORT_SYMBOL(release_resource); | 332 | EXPORT_SYMBOL(release_resource); |
324 | 333 | ||
325 | #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY) | ||
326 | /* | 334 | /* |
327 | * Finds the lowest memory reosurce exists within [res->start.res->end) | 335 | * Finds the lowest iomem reosurce exists with-in [res->start.res->end) |
328 | * the caller must specify res->start, res->end, res->flags and "name". | 336 | * the caller must specify res->start, res->end, res->flags and "name". |
329 | * If found, returns 0, res is overwritten, if not found, returns -1. | 337 | * If found, returns 0, res is overwritten, if not found, returns -1. |
338 | * This walks through whole tree and not just first level children | ||
339 | * until and unless first_level_children_only is true. | ||
330 | */ | 340 | */ |
331 | static int find_next_system_ram(struct resource *res, char *name) | 341 | static int find_next_iomem_res(struct resource *res, char *name, |
342 | bool first_level_children_only) | ||
332 | { | 343 | { |
333 | resource_size_t start, end; | 344 | resource_size_t start, end; |
334 | struct resource *p; | 345 | struct resource *p; |
346 | bool sibling_only = false; | ||
335 | 347 | ||
336 | BUG_ON(!res); | 348 | BUG_ON(!res); |
337 | 349 | ||
@@ -340,8 +352,14 @@ static int find_next_system_ram(struct resource *res, char *name) | |||
340 | BUG_ON(start >= end); | 352 | BUG_ON(start >= end); |
341 | 353 | ||
342 | read_lock(&resource_lock); | 354 | read_lock(&resource_lock); |
343 | for (p = iomem_resource.child; p ; p = p->sibling) { | 355 | |
344 | /* system ram is just marked as IORESOURCE_MEM */ | 356 | if (first_level_children_only) { |
357 | p = iomem_resource.child; | ||
358 | sibling_only = true; | ||
359 | } else | ||
360 | p = &iomem_resource; | ||
361 | |||
362 | while ((p = next_resource(p, sibling_only))) { | ||
345 | if (p->flags != res->flags) | 363 | if (p->flags != res->flags) |
346 | continue; | 364 | continue; |
347 | if (name && strcmp(p->name, name)) | 365 | if (name && strcmp(p->name, name)) |
@@ -353,6 +371,7 @@ static int find_next_system_ram(struct resource *res, char *name) | |||
353 | if ((p->end >= start) && (p->start < end)) | 371 | if ((p->end >= start) && (p->start < end)) |
354 | break; | 372 | break; |
355 | } | 373 | } |
374 | |||
356 | read_unlock(&resource_lock); | 375 | read_unlock(&resource_lock); |
357 | if (!p) | 376 | if (!p) |
358 | return -1; | 377 | return -1; |
@@ -365,6 +384,70 @@ static int find_next_system_ram(struct resource *res, char *name) | |||
365 | } | 384 | } |
366 | 385 | ||
367 | /* | 386 | /* |
387 | * Walks through iomem resources and calls func() with matching resource | ||
388 | * ranges. This walks through whole tree and not just first level children. | ||
389 | * All the memory ranges which overlap start,end and also match flags and | ||
390 | * name are valid candidates. | ||
391 | * | ||
392 | * @name: name of resource | ||
393 | * @flags: resource flags | ||
394 | * @start: start addr | ||
395 | * @end: end addr | ||
396 | */ | ||
397 | int walk_iomem_res(char *name, unsigned long flags, u64 start, u64 end, | ||
398 | void *arg, int (*func)(u64, u64, void *)) | ||
399 | { | ||
400 | struct resource res; | ||
401 | u64 orig_end; | ||
402 | int ret = -1; | ||
403 | |||
404 | res.start = start; | ||
405 | res.end = end; | ||
406 | res.flags = flags; | ||
407 | orig_end = res.end; | ||
408 | while ((res.start < res.end) && | ||
409 | (!find_next_iomem_res(&res, name, false))) { | ||
410 | ret = (*func)(res.start, res.end, arg); | ||
411 | if (ret) | ||
412 | break; | ||
413 | res.start = res.end + 1; | ||
414 | res.end = orig_end; | ||
415 | } | ||
416 | return ret; | ||
417 | } | ||
418 | |||
419 | /* | ||
420 | * This function calls callback against all memory range of "System RAM" | ||
421 | * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY. | ||
422 | * Now, this function is only for "System RAM". This function deals with | ||
423 | * full ranges and not pfn. If resources are not pfn aligned, dealing | ||
424 | * with pfn can truncate ranges. | ||
425 | */ | ||
426 | int walk_system_ram_res(u64 start, u64 end, void *arg, | ||
427 | int (*func)(u64, u64, void *)) | ||
428 | { | ||
429 | struct resource res; | ||
430 | u64 orig_end; | ||
431 | int ret = -1; | ||
432 | |||
433 | res.start = start; | ||
434 | res.end = end; | ||
435 | res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; | ||
436 | orig_end = res.end; | ||
437 | while ((res.start < res.end) && | ||
438 | (!find_next_iomem_res(&res, "System RAM", true))) { | ||
439 | ret = (*func)(res.start, res.end, arg); | ||
440 | if (ret) | ||
441 | break; | ||
442 | res.start = res.end + 1; | ||
443 | res.end = orig_end; | ||
444 | } | ||
445 | return ret; | ||
446 | } | ||
447 | |||
448 | #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY) | ||
449 | |||
450 | /* | ||
368 | * This function calls callback against all memory range of "System RAM" | 451 | * This function calls callback against all memory range of "System RAM" |
369 | * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY. | 452 | * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY. |
370 | * Now, this function is only for "System RAM". | 453 | * Now, this function is only for "System RAM". |
@@ -382,7 +465,7 @@ int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages, | |||
382 | res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; | 465 | res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
383 | orig_end = res.end; | 466 | orig_end = res.end; |
384 | while ((res.start < res.end) && | 467 | while ((res.start < res.end) && |
385 | (find_next_system_ram(&res, "System RAM") >= 0)) { | 468 | (find_next_iomem_res(&res, "System RAM", true) >= 0)) { |
386 | pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT; | 469 | pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT; |
387 | end_pfn = (res.end + 1) >> PAGE_SHIFT; | 470 | end_pfn = (res.end + 1) >> PAGE_SHIFT; |
388 | if (end_pfn > pfn) | 471 | if (end_pfn > pfn) |