Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | #include <linux/rculist.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/io.h> |
| 19 | #include "nfit_test.h" |
| 20 | |
| 21 | static LIST_HEAD(iomap_head); |
| 22 | |
| 23 | static struct iomap_ops { |
| 24 | nfit_test_lookup_fn nfit_test_lookup; |
| 25 | struct list_head list; |
| 26 | } iomap_ops = { |
| 27 | .list = LIST_HEAD_INIT(iomap_ops.list), |
| 28 | }; |
| 29 | |
| 30 | void nfit_test_setup(nfit_test_lookup_fn lookup) |
| 31 | { |
| 32 | iomap_ops.nfit_test_lookup = lookup; |
| 33 | list_add_rcu(&iomap_ops.list, &iomap_head); |
| 34 | } |
| 35 | EXPORT_SYMBOL(nfit_test_setup); |
| 36 | |
| 37 | void nfit_test_teardown(void) |
| 38 | { |
| 39 | list_del_rcu(&iomap_ops.list); |
| 40 | synchronize_rcu(); |
| 41 | } |
| 42 | EXPORT_SYMBOL(nfit_test_teardown); |
| 43 | |
| 44 | static struct nfit_test_resource *get_nfit_res(resource_size_t resource) |
| 45 | { |
| 46 | struct iomap_ops *ops; |
| 47 | |
| 48 | ops = list_first_or_null_rcu(&iomap_head, typeof(*ops), list); |
| 49 | if (ops) |
| 50 | return ops->nfit_test_lookup(resource); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | void __iomem *__nfit_test_ioremap(resource_size_t offset, unsigned long size, |
| 55 | void __iomem *(*fallback_fn)(resource_size_t, unsigned long)) |
| 56 | { |
| 57 | struct nfit_test_resource *nfit_res; |
| 58 | |
| 59 | rcu_read_lock(); |
| 60 | nfit_res = get_nfit_res(offset); |
| 61 | rcu_read_unlock(); |
| 62 | if (nfit_res) |
| 63 | return (void __iomem *) nfit_res->buf + offset |
| 64 | - nfit_res->res->start; |
| 65 | return fallback_fn(offset, size); |
| 66 | } |
| 67 | |
Dan Williams | 9d27a87 | 2015-07-10 14:07:03 -0400 | [diff] [blame] | 68 | void __iomem *__wrap_devm_ioremap_nocache(struct device *dev, |
| 69 | resource_size_t offset, unsigned long size) |
| 70 | { |
| 71 | struct nfit_test_resource *nfit_res; |
| 72 | |
| 73 | rcu_read_lock(); |
| 74 | nfit_res = get_nfit_res(offset); |
| 75 | rcu_read_unlock(); |
| 76 | if (nfit_res) |
| 77 | return (void __iomem *) nfit_res->buf + offset |
| 78 | - nfit_res->res->start; |
| 79 | return devm_ioremap_nocache(dev, offset, size); |
| 80 | } |
| 81 | EXPORT_SYMBOL(__wrap_devm_ioremap_nocache); |
| 82 | |
Dan Williams | e836a25 | 2015-08-12 18:42:56 -0400 | [diff] [blame^] | 83 | void *__wrap_memremap(resource_size_t offset, size_t size, |
| 84 | unsigned long flags) |
Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 85 | { |
Dan Williams | e836a25 | 2015-08-12 18:42:56 -0400 | [diff] [blame^] | 86 | struct nfit_test_resource *nfit_res; |
| 87 | |
| 88 | rcu_read_lock(); |
| 89 | nfit_res = get_nfit_res(offset); |
| 90 | rcu_read_unlock(); |
| 91 | if (nfit_res) |
| 92 | return (void __iomem *) nfit_res->buf + offset |
| 93 | - nfit_res->res->start; |
| 94 | return memremap(offset, size, flags); |
Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 95 | } |
Dan Williams | e836a25 | 2015-08-12 18:42:56 -0400 | [diff] [blame^] | 96 | EXPORT_SYMBOL(__wrap_memremap); |
Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 97 | |
| 98 | void __iomem *__wrap_ioremap_nocache(resource_size_t offset, unsigned long size) |
| 99 | { |
| 100 | return __nfit_test_ioremap(offset, size, ioremap_nocache); |
| 101 | } |
| 102 | EXPORT_SYMBOL(__wrap_ioremap_nocache); |
| 103 | |
Dan Williams | 9d27a87 | 2015-07-10 14:07:03 -0400 | [diff] [blame] | 104 | void __iomem *__wrap_ioremap_wc(resource_size_t offset, unsigned long size) |
| 105 | { |
| 106 | return __nfit_test_ioremap(offset, size, ioremap_wc); |
| 107 | } |
| 108 | EXPORT_SYMBOL(__wrap_ioremap_wc); |
| 109 | |
Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 110 | void __wrap_iounmap(volatile void __iomem *addr) |
| 111 | { |
| 112 | struct nfit_test_resource *nfit_res; |
| 113 | |
| 114 | rcu_read_lock(); |
| 115 | nfit_res = get_nfit_res((unsigned long) addr); |
| 116 | rcu_read_unlock(); |
| 117 | if (nfit_res) |
| 118 | return; |
| 119 | return iounmap(addr); |
| 120 | } |
| 121 | EXPORT_SYMBOL(__wrap_iounmap); |
| 122 | |
Dan Williams | e836a25 | 2015-08-12 18:42:56 -0400 | [diff] [blame^] | 123 | void __wrap_memunmap(void *addr) |
| 124 | { |
| 125 | struct nfit_test_resource *nfit_res; |
| 126 | |
| 127 | rcu_read_lock(); |
| 128 | nfit_res = get_nfit_res((unsigned long) addr); |
| 129 | rcu_read_unlock(); |
| 130 | if (nfit_res) |
| 131 | return; |
| 132 | return memunmap(addr); |
| 133 | } |
| 134 | EXPORT_SYMBOL(__wrap_memunmap); |
| 135 | |
Dan Williams | 6bc7561 | 2015-06-17 17:23:32 -0400 | [diff] [blame] | 136 | struct resource *__wrap___request_region(struct resource *parent, |
| 137 | resource_size_t start, resource_size_t n, const char *name, |
| 138 | int flags) |
| 139 | { |
| 140 | struct nfit_test_resource *nfit_res; |
| 141 | |
| 142 | if (parent == &iomem_resource) { |
| 143 | rcu_read_lock(); |
| 144 | nfit_res = get_nfit_res(start); |
| 145 | rcu_read_unlock(); |
| 146 | if (nfit_res) { |
| 147 | struct resource *res = nfit_res->res + 1; |
| 148 | |
| 149 | if (start + n > nfit_res->res->start |
| 150 | + resource_size(nfit_res->res)) { |
| 151 | pr_debug("%s: start: %llx n: %llx overflow: %pr\n", |
| 152 | __func__, start, n, |
| 153 | nfit_res->res); |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | res->start = start; |
| 158 | res->end = start + n - 1; |
| 159 | res->name = name; |
| 160 | res->flags = resource_type(parent); |
| 161 | res->flags |= IORESOURCE_BUSY | flags; |
| 162 | pr_debug("%s: %pr\n", __func__, res); |
| 163 | return res; |
| 164 | } |
| 165 | } |
| 166 | return __request_region(parent, start, n, name, flags); |
| 167 | } |
| 168 | EXPORT_SYMBOL(__wrap___request_region); |
| 169 | |
| 170 | void __wrap___release_region(struct resource *parent, resource_size_t start, |
| 171 | resource_size_t n) |
| 172 | { |
| 173 | struct nfit_test_resource *nfit_res; |
| 174 | |
| 175 | if (parent == &iomem_resource) { |
| 176 | rcu_read_lock(); |
| 177 | nfit_res = get_nfit_res(start); |
| 178 | rcu_read_unlock(); |
| 179 | if (nfit_res) { |
| 180 | struct resource *res = nfit_res->res + 1; |
| 181 | |
| 182 | if (start != res->start || resource_size(res) != n) |
| 183 | pr_info("%s: start: %llx n: %llx mismatch: %pr\n", |
| 184 | __func__, start, n, res); |
| 185 | else |
| 186 | memset(res, 0, sizeof(*res)); |
| 187 | return; |
| 188 | } |
| 189 | } |
| 190 | __release_region(parent, start, n); |
| 191 | } |
| 192 | EXPORT_SYMBOL(__wrap___release_region); |
| 193 | |
| 194 | MODULE_LICENSE("GPL v2"); |