--- zzzz-none-000/linux-3.10.107/kernel/resource.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/kernel/resource.c 2021-02-04 17:41:59.000000000 +0000 @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -41,6 +42,89 @@ }; EXPORT_SYMBOL(iomem_resource); +#ifdef CONFIG_AVM_ENHANCED +#ifdef CONFIG_MIPS_UR8 +struct resource nwss_tx_queue_resource = { + .name = "NWSS Tx Queue", + .start = 0, + .end = 17, + .flags = IORESOURCE_DMA +}; +EXPORT_SYMBOL(nwss_tx_queue_resource); + +struct resource nwss_tx_completion_queue_resource = { + .name = "NWSS Tx Completion Queue", + .start = 0, + .end = 3, + .flags = IORESOURCE_DMA +}; +EXPORT_SYMBOL(nwss_tx_completion_queue_resource); + +struct resource nwss_rx_queue_resource = { + .name = "NWSS Rx Queue", + .start = 0, + .end = 7, + .flags = IORESOURCE_DMA +}; +EXPORT_SYMBOL(nwss_rx_queue_resource); + +struct resource nwss_free_buffer_queue_resource = { + .name = "NWSS Free Buffer Descriptor Queue", + .start = 0, + .end = 3, + .flags = IORESOURCE_DMA +}; +EXPORT_SYMBOL(nwss_free_buffer_queue_resource); + +struct resource nwss_free_packet_queue_resource = { + .name = "NWSS Free Packet Descriptor Queue", + .start = 0, + .end = 1, + .flags = IORESOURCE_DMA +}; +EXPORT_SYMBOL(nwss_free_packet_queue_resource); + +struct resource timer_resource = { + .name = "timer", + .start = 0, + .end = 3, + .flags = IORESOURCE_IO +}; +EXPORT_SYMBOL(timer_resource); +#endif /*--- #ifdef CONFIG_MIPS_UR8 ---*/ + +struct resource sflash_resource = { + .name = "SFLASH", + .start = 0, + .end = 1024*1024, + .flags = IORESOURCE_MEM, +}; +EXPORT_SYMBOL(sflash_resource); + +struct resource nand_flash_resource = { + .name = "NAND", + .start = 0, + .end = 1024*1024, /*--- Angaben in MByte ---*/ + .flags = IORESOURCE_MEM, +}; +EXPORT_SYMBOL(nand_flash_resource); + +struct resource gpio_resource = { + .name = "gpio", + .start = 0, +# if defined (CONFIG_SOC_QCA955X) || defined(CONFIG_SOC_AR934X) || defined (CONFIG_SOC_QCA953X) + .end = 116, /*--- 0..22, Shiftregister: 101..116 ---*/ +# elif defined (CONFIG_ARCH_IPQ806X_DT) + .end = 69, +# else /*--- #if defined (CONFIG_SOC_QCA955X) || defined(CONFIG_SOC_AR934X) ---*/ + .end = 64, +# endif /*--- #else ---*/ /*--- #if defined (CONFIG_SOC_QCA955X) || defined(CONFIG_SOC_AR934X) ---*/ + .flags = IORESOURCE_IO +}; +EXPORT_SYMBOL(gpio_resource); + +#endif /* CONFIG_AVM_ENHANCED */ + /* constraints to be met while allocating resources */ struct resource_constraint { resource_size_t min, max, align; @@ -59,10 +143,12 @@ static struct resource *bootmem_resource_free; static DEFINE_SPINLOCK(bootmem_resource_lock); -static void *r_next(struct seq_file *m, void *v, loff_t *pos) +static struct resource *next_resource(struct resource *p, bool sibling_only) { - struct resource *p = v; - (*pos)++; + /* Caller wants to traverse through siblings only */ + if (sibling_only) + return p->sibling; + if (p->child) return p->child; while (!p->sibling && p->parent) @@ -70,6 +156,13 @@ return p->sibling; } +static void *r_next(struct seq_file *m, void *v, loff_t *pos) +{ + struct resource *p = v; + (*pos)++; + return (void *)next_resource(p, false); +} + #ifdef CONFIG_PROC_FS enum { MAX_IORES_LEVEL = 5 }; @@ -95,16 +188,25 @@ { struct resource *root = m->private; struct resource *r = v, *p; + unsigned long long start, end; int width = root->end < 0x10000 ? 4 : 8; int depth; for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent) if (p->parent == root) break; + + if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) { + start = r->start; + end = r->end; + } else { + start = end = 0; + } + seq_printf(m, "%*s%0*llx-%0*llx : %s\n", depth * 2, "", - width, (unsigned long long) r->start, - width, (unsigned long long) r->end, + width, start, + width, end, r->name ? r->name : ""); return 0; } @@ -150,16 +252,67 @@ .release = seq_release, }; +#ifdef CONFIG_AVM_ENHANCED +static int iogpio_open(struct inode *inode, struct file *file) +{ + int res = seq_open(file, &resource_op); + if (!res) { + struct seq_file *m = file->private_data; + m->private = &gpio_resource; + } + return res; +} + +static struct file_operations proc_gpio_operations = { + .open = iogpio_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, +}; +#endif /* CONFIG_AVM_ENHANCED */ + static int __init ioresources_init(void) { proc_create("ioports", 0, NULL, &proc_ioports_operations); - proc_create("iomem", 0, NULL, &proc_iomem_operations); + proc_create("iomem", S_IRUSR, NULL, &proc_iomem_operations); +#ifdef CONFIG_AVM_ENHANCED + proc_create("gpio", 0, NULL, &proc_gpio_operations); +#endif + return 0; } __initcall(ioresources_init); #endif /* CONFIG_PROC_FS */ +#ifdef CONFIG_AVM_ENHANCED +void print_resource_tree(struct resource *root __attribute__ ((unused)), unsigned int level __attribute__ ((unused))) +{ +#if defined(DEBUG_RESOURCE) + static const char *indent[] = { + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " " + }; + + if (level == 0) + printk(KERN_ERR "[resource-tree] "); + while (root) { + printk(KERN_ERR "%d:%s[%s] (0x%x - 0x%x)\n", level, indent[level], root->name, root->start, root->end); + if (root->child) { + print_resource_tree(root->child, level + 1); + } + root = root->sibling; + } +#endif /*--- #if defined(DEBUG_RESOURCE) ---*/ +} +#endif + static void free_resource(struct resource *res) { if (!res) @@ -200,13 +353,30 @@ resource_size_t start = new->start; resource_size_t end = new->end; struct resource *tmp, **p; +#if defined(CONFIG_AVM_ENHANCED) && defined(DEBUG_RESOURCE) + const int debug_resource = 1; +#else + const int debug_resource = 0; +#endif - if (end < start) + if (end < start) { + if (debug_resource) + pr_err("[request_resource] %s: end 0x%x < start " + "0x%x\n", new->name, end, start); return root; - if (start < root->start) + } + if (start < root->start) { + if (debug_resource) + pr_err("[request_resource] %s: start 0x%x < root->start " + "0x%x\n", new->name, start, root->start); return root; - if (end > root->end) + } + if (end > root->end) { + if (debug_resource) + pr_err("[request_resource] %s: end 0x%x > root->end " + "0x%x\n", new->name, end, root->end); return root; + } p = &root->child; for (;;) { tmp = *p; @@ -322,16 +492,19 @@ EXPORT_SYMBOL(release_resource); -#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY) /* - * Finds the lowest memory reosurce exists within [res->start.res->end) + * Finds the lowest iomem reosurce exists with-in [res->start.res->end) * the caller must specify res->start, res->end, res->flags and "name". * If found, returns 0, res is overwritten, if not found, returns -1. + * This walks through whole tree and not just first level children + * until and unless first_level_children_only is true. */ -static int find_next_system_ram(struct resource *res, char *name) +static int find_next_iomem_res(struct resource *res, char *name, + bool first_level_children_only) { resource_size_t start, end; struct resource *p; + bool sibling_only = false; BUG_ON(!res); @@ -339,9 +512,12 @@ end = res->end; BUG_ON(start >= end); + if (first_level_children_only) + sibling_only = true; + read_lock(&resource_lock); - for (p = iomem_resource.child; p ; p = p->sibling) { - /* system ram is just marked as IORESOURCE_MEM */ + + for (p = iomem_resource.child; p; p = next_resource(p, sibling_only)) { if (p->flags != res->flags) continue; if (name && strcmp(p->name, name)) @@ -353,6 +529,7 @@ if ((p->end >= start) && (p->start < end)) break; } + read_unlock(&resource_lock); if (!p) return -1; @@ -365,6 +542,71 @@ } /* + * Walks through iomem resources and calls func() with matching resource + * ranges. This walks through whole tree and not just first level children. + * All the memory ranges which overlap start,end and also match flags and + * name are valid candidates. + * + * @name: name of resource + * @flags: resource flags + * @start: start addr + * @end: end addr + */ +int walk_iomem_res(char *name, unsigned long flags, u64 start, u64 end, + void *arg, int (*func)(u64, u64, void *)) +{ + struct resource res; + u64 orig_end; + int ret = -1; + + res.start = start; + res.end = end; + res.flags = flags; + orig_end = res.end; + while ((res.start < res.end) && + (!find_next_iomem_res(&res, name, false))) { + ret = (*func)(res.start, res.end, arg); + if (ret) + break; + res.start = res.end + 1; + res.end = orig_end; + } + return ret; +} + +/* + * This function calls callback against all memory range of "System RAM" + * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY. + * Now, this function is only for "System RAM". This function deals with + * full ranges and not pfn. If resources are not pfn aligned, dealing + * with pfn can truncate ranges. + */ +int walk_system_ram_res(u64 start, u64 end, void *arg, + int (*func)(u64, u64, void *)) +{ + struct resource res; + u64 orig_end; + int ret = -1; + + res.start = start; + res.end = end; + res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; + orig_end = res.end; + while ((res.start < res.end) && + (!find_next_iomem_res(&res, "System RAM", true))) { + ret = (*func)(res.start, res.end, arg); + if (ret) + break; + res.start = res.end + 1; + res.end = orig_end; + } + return ret; +} +EXPORT_SYMBOL_GPL(walk_system_ram_res); + +#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY) + +/* * This function calls callback against all memory range of "System RAM" * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY. * Now, this function is only for "System RAM". @@ -382,7 +624,7 @@ res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; orig_end = res.end; while ((res.start < res.end) && - (find_next_system_ram(&res, "System RAM") >= 0)) { + (find_next_iomem_res(&res, "System RAM", true) >= 0)) { pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT; end_pfn = (res.end + 1) >> PAGE_SHIFT; if (end_pfn > pfn) @@ -409,6 +651,54 @@ { return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1; } +EXPORT_SYMBOL_GPL(page_is_ram); + +/** + * region_intersects() - determine intersection of region with known resources + * @start: region start address + * @size: size of region + * @name: name of resource (in iomem_resource) + * + * Check if the specified region partially overlaps or fully eclipses a + * resource identified by @name. Return REGION_DISJOINT if the region + * does not overlap @name, return REGION_MIXED if the region overlaps + * @type and another resource, and return REGION_INTERSECTS if the + * region overlaps @type and no other defined resource. Note, that + * REGION_INTERSECTS is also returned in the case when the specified + * region overlaps RAM and undefined memory holes. + * + * region_intersect() is used by memory remapping functions to ensure + * the user is not remapping RAM and is a vast speed up over walking + * through the resource table page by page. + */ +int region_intersects(resource_size_t start, size_t size, const char *name) +{ + unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY; + resource_size_t end = start + size - 1; + int type = 0; int other = 0; + struct resource *p; + + read_lock(&resource_lock); + for (p = iomem_resource.child; p ; p = p->sibling) { + bool is_type = strcmp(p->name, name) == 0 && p->flags == flags; + + if (start >= p->start && start <= p->end) + is_type ? type++ : other++; + if (end >= p->start && end <= p->end) + is_type ? type++ : other++; + if (p->start >= start && p->end <= end) + is_type ? type++ : other++; + } + read_unlock(&resource_lock); + + if (other == 0) + return type ? REGION_INTERSECTS : REGION_DISJOINT; + + if (type) + return REGION_MIXED; + + return REGION_DISJOINT; +} void __weak arch_remove_reservations(struct resource *avail) { @@ -431,11 +721,6 @@ res->end = max; } -static bool resource_contains(struct resource *res1, struct resource *res2) -{ - return res1->start <= res2->start && res1->end >= res2->end; -} - /* * Find empty slot in the resource tree with the given range and * alignment constraints @@ -471,10 +756,11 @@ arch_remove_reservations(&tmp); /* Check for overflow after ALIGN() */ - avail = *new; avail.start = ALIGN(tmp.start, constraint->align); avail.end = tmp.end; + avail.flags = new->flags & ~IORESOURCE_UNSET; if (avail.start >= tmp.start) { + alloc.flags = avail.flags; alloc.start = constraint->alignf(constraint->alignf_data, &avail, size, constraint->align); alloc.end = alloc.start + size - 1; @@ -515,7 +801,7 @@ * @newsize: new size of the resource descriptor * @constraint: the size and alignment constraints to be met. */ -int reallocate_resource(struct resource *root, struct resource *old, +static int reallocate_resource(struct resource *root, struct resource *old, resource_size_t newsize, struct resource_constraint *constraint) { @@ -921,8 +1207,6 @@ * * request_region creates a new busy region. * - * check_region returns non-zero if the area is already busy. - * * release_region releases a matching busy region. */ @@ -949,8 +1233,8 @@ res->name = name; res->start = start; res->end = start + n - 1; - res->flags = IORESOURCE_BUSY; - res->flags |= flags; + res->flags = resource_type(parent); + res->flags |= IORESOURCE_BUSY | flags; write_lock(&resource_lock); @@ -986,36 +1270,6 @@ EXPORT_SYMBOL(__request_region); /** - * __check_region - check if a resource region is busy or free - * @parent: parent resource descriptor - * @start: resource start address - * @n: resource region size - * - * Returns 0 if the region is free at the moment it is checked, - * returns %-EBUSY if the region is busy. - * - * NOTE: - * This function is deprecated because its use is racy. - * Even if it returns 0, a subsequent call to request_region() - * may fail because another driver etc. just allocated the region. - * Do NOT use it. It will be removed from the kernel. - */ -int __check_region(struct resource *parent, resource_size_t start, - resource_size_t n) -{ - struct resource * res; - - res = __request_region(parent, start, n, "check-region", 0); - if (!res) - return -EBUSY; - - release_resource(res); - free_resource(res); - return 0; -} -EXPORT_SYMBOL(__check_region); - -/** * __release_region - release a previously reserved resource region * @parent: parent resource descriptor * @start: resource start address @@ -1170,6 +1424,76 @@ /* * Managed region resource */ +static void devm_resource_release(struct device *dev, void *ptr) +{ + struct resource **r = ptr; + + release_resource(*r); +} + +/** + * devm_request_resource() - request and reserve an I/O or memory resource + * @dev: device for which to request the resource + * @root: root of the resource tree from which to request the resource + * @new: descriptor of the resource to request + * + * This is a device-managed version of request_resource(). There is usually + * no need to release resources requested by this function explicitly since + * that will be taken care of when the device is unbound from its driver. + * If for some reason the resource needs to be released explicitly, because + * of ordering issues for example, drivers must call devm_release_resource() + * rather than the regular release_resource(). + * + * When a conflict is detected between any existing resources and the newly + * requested resource, an error message will be printed. + * + * Returns 0 on success or a negative error code on failure. + */ +int devm_request_resource(struct device *dev, struct resource *root, + struct resource *new) +{ + struct resource *conflict, **ptr; + + ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + *ptr = new; + + conflict = request_resource_conflict(root, new); + if (conflict) { + dev_err(dev, "resource collision: %pR conflicts with %s %pR\n", + new, conflict->name, conflict); + devres_free(ptr); + return -EBUSY; + } + + devres_add(dev, ptr); + return 0; +} +EXPORT_SYMBOL(devm_request_resource); + +static int devm_resource_match(struct device *dev, void *res, void *data) +{ + struct resource **ptr = res; + + return *ptr == data; +} + +/** + * devm_release_resource() - release a previously requested resource + * @dev: device for which to release the resource + * @new: descriptor of the resource to release + * + * Releases a resource previously requested using devm_request_resource(). + */ +void devm_release_resource(struct device *dev, struct resource *new) +{ + WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match, + new)); +} +EXPORT_SYMBOL(devm_release_resource); + struct region_devres { struct resource *parent; resource_size_t start; @@ -1293,13 +1617,10 @@ if (p->flags & IORESOURCE_BUSY) continue; - printk(KERN_WARNING "resource map sanity check conflict: " - "0x%llx 0x%llx 0x%llx 0x%llx %s\n", + printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n", (unsigned long long)addr, (unsigned long long)(addr + size - 1), - (unsigned long long)p->start, - (unsigned long long)p->end, - p->name); + p->name, p); err = -1; break; } @@ -1351,6 +1672,30 @@ return err; } +struct resource_entry *resource_list_create_entry(struct resource *res, + size_t extra_size) +{ + struct resource_entry *entry; + + entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL); + if (entry) { + INIT_LIST_HEAD(&entry->node); + entry->res = res ? res : &entry->__res; + } + + return entry; +} +EXPORT_SYMBOL(resource_list_create_entry); + +void resource_list_free(struct list_head *head) +{ + struct resource_entry *entry, *tmp; + + list_for_each_entry_safe(entry, tmp, head, node) + resource_list_destroy_entry(entry); +} +EXPORT_SYMBOL(resource_list_free); + static int __init strict_iomem(char *str) { if (strstr(str, "relaxed"))