2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
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>
20 #include "nfit_test.h"
22 static LIST_HEAD(iomap_head);
24 static struct iomap_ops {
25 nfit_test_lookup_fn nfit_test_lookup;
26 struct list_head list;
28 .list = LIST_HEAD_INIT(iomap_ops.list),
31 void nfit_test_setup(nfit_test_lookup_fn lookup)
33 iomap_ops.nfit_test_lookup = lookup;
34 list_add_rcu(&iomap_ops.list, &iomap_head);
36 EXPORT_SYMBOL(nfit_test_setup);
38 void nfit_test_teardown(void)
40 list_del_rcu(&iomap_ops.list);
43 EXPORT_SYMBOL(nfit_test_teardown);
45 static struct nfit_test_resource *__get_nfit_res(resource_size_t resource)
47 struct iomap_ops *ops;
49 ops = list_first_or_null_rcu(&iomap_head, typeof(*ops), list);
51 return ops->nfit_test_lookup(resource);
55 static struct nfit_test_resource *get_nfit_res(resource_size_t resource)
57 struct nfit_test_resource *res;
60 res = __get_nfit_res(resource);
66 void __iomem *__nfit_test_ioremap(resource_size_t offset, unsigned long size,
67 void __iomem *(*fallback_fn)(resource_size_t, unsigned long))
69 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
72 return (void __iomem *) nfit_res->buf + offset
73 - nfit_res->res->start;
74 return fallback_fn(offset, size);
77 void __iomem *__wrap_devm_ioremap_nocache(struct device *dev,
78 resource_size_t offset, unsigned long size)
80 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
83 return (void __iomem *) nfit_res->buf + offset
84 - nfit_res->res->start;
85 return devm_ioremap_nocache(dev, offset, size);
87 EXPORT_SYMBOL(__wrap_devm_ioremap_nocache);
89 void *__wrap_devm_memremap(struct device *dev, resource_size_t offset,
90 size_t size, unsigned long flags)
92 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
95 return nfit_res->buf + offset - nfit_res->res->start;
96 return devm_memremap(dev, offset, size, flags);
98 EXPORT_SYMBOL(__wrap_devm_memremap);
100 #ifdef __HAVE_ARCH_PTE_DEVMAP
101 #include <linux/memremap.h>
102 #include <linux/pfn_t.h>
104 void *__wrap_devm_memremap_pages(struct device *dev, struct resource *res,
105 struct percpu_ref *ref, struct vmem_altmap *altmap)
107 resource_size_t offset = res->start;
108 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
111 return nfit_res->buf + offset - nfit_res->res->start;
112 return devm_memremap_pages(dev, res, ref, altmap);
114 EXPORT_SYMBOL(__wrap_devm_memremap_pages);
116 pfn_t __wrap_phys_to_pfn_t(phys_addr_t addr, unsigned long flags)
118 struct nfit_test_resource *nfit_res = get_nfit_res(addr);
122 return phys_to_pfn_t(addr, flags);
124 EXPORT_SYMBOL(__wrap_phys_to_pfn_t);
126 /* to be removed post 4.5-rc1 */
127 void *__wrap_devm_memremap_pages(struct device *dev, struct resource *res)
129 resource_size_t offset = res->start;
130 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
133 return nfit_res->buf + offset - nfit_res->res->start;
134 return devm_memremap_pages(dev, res);
136 EXPORT_SYMBOL(__wrap_devm_memremap_pages);
139 void *__wrap_memremap(resource_size_t offset, size_t size,
142 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
145 return nfit_res->buf + offset - nfit_res->res->start;
146 return memremap(offset, size, flags);
148 EXPORT_SYMBOL(__wrap_memremap);
150 void __wrap_devm_memunmap(struct device *dev, void *addr)
152 struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
156 return devm_memunmap(dev, addr);
158 EXPORT_SYMBOL(__wrap_devm_memunmap);
160 void __iomem *__wrap_ioremap_nocache(resource_size_t offset, unsigned long size)
162 return __nfit_test_ioremap(offset, size, ioremap_nocache);
164 EXPORT_SYMBOL(__wrap_ioremap_nocache);
166 void __iomem *__wrap_ioremap_wc(resource_size_t offset, unsigned long size)
168 return __nfit_test_ioremap(offset, size, ioremap_wc);
170 EXPORT_SYMBOL(__wrap_ioremap_wc);
172 void __wrap_iounmap(volatile void __iomem *addr)
174 struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
177 return iounmap(addr);
179 EXPORT_SYMBOL(__wrap_iounmap);
181 void __wrap_memunmap(void *addr)
183 struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
187 return memunmap(addr);
189 EXPORT_SYMBOL(__wrap_memunmap);
191 static struct resource *nfit_test_request_region(struct device *dev,
192 struct resource *parent, resource_size_t start,
193 resource_size_t n, const char *name, int flags)
195 struct nfit_test_resource *nfit_res;
197 if (parent == &iomem_resource) {
198 nfit_res = get_nfit_res(start);
200 struct resource *res = nfit_res->res + 1;
202 if (start + n > nfit_res->res->start
203 + resource_size(nfit_res->res)) {
204 pr_debug("%s: start: %llx n: %llx overflow: %pr\n",
211 res->end = start + n - 1;
213 res->flags = resource_type(parent);
214 res->flags |= IORESOURCE_BUSY | flags;
215 pr_debug("%s: %pr\n", __func__, res);
220 return __devm_request_region(dev, parent, start, n, name);
221 return __request_region(parent, start, n, name, flags);
224 struct resource *__wrap___request_region(struct resource *parent,
225 resource_size_t start, resource_size_t n, const char *name,
228 return nfit_test_request_region(NULL, parent, start, n, name, flags);
230 EXPORT_SYMBOL(__wrap___request_region);
232 struct resource *__wrap___devm_request_region(struct device *dev,
233 struct resource *parent, resource_size_t start,
234 resource_size_t n, const char *name)
238 return nfit_test_request_region(dev, parent, start, n, name, 0);
240 EXPORT_SYMBOL(__wrap___devm_request_region);
242 void __wrap___release_region(struct resource *parent, resource_size_t start,
245 struct nfit_test_resource *nfit_res;
247 if (parent == &iomem_resource) {
248 nfit_res = get_nfit_res(start);
250 struct resource *res = nfit_res->res + 1;
252 if (start != res->start || resource_size(res) != n)
253 pr_info("%s: start: %llx n: %llx mismatch: %pr\n",
254 __func__, start, n, res);
256 memset(res, 0, sizeof(*res));
260 __release_region(parent, start, n);
262 EXPORT_SYMBOL(__wrap___release_region);
264 MODULE_LICENSE("GPL v2");