blob: 179d2289f3a8289874edccfe6ffcbe8cffd058e4 [file] [log] [blame]
Dan Williams6bc75612015-06-17 17:23:32 -04001/*
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
21static LIST_HEAD(iomap_head);
22
23static 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
30void 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}
35EXPORT_SYMBOL(nfit_test_setup);
36
37void nfit_test_teardown(void)
38{
39 list_del_rcu(&iomap_ops.list);
40 synchronize_rcu();
41}
42EXPORT_SYMBOL(nfit_test_teardown);
43
44static 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
54void __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 Williams9d27a872015-07-10 14:07:03 -040068void __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}
81EXPORT_SYMBOL(__wrap_devm_ioremap_nocache);
82
Christoph Hellwig708ab622015-08-10 23:07:08 -040083void *__wrap_devm_memremap(struct device *dev, resource_size_t offset,
84 size_t size, unsigned long flags)
Dan Williams6bc75612015-06-17 17:23:32 -040085{
Dan Williamse836a252015-08-12 18:42:56 -040086 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)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -060092 return nfit_res->buf + offset - nfit_res->res->start;
Christoph Hellwig708ab622015-08-10 23:07:08 -040093 return devm_memremap(dev, offset, size, flags);
Dan Williams6bc75612015-06-17 17:23:32 -040094}
Christoph Hellwig708ab622015-08-10 23:07:08 -040095EXPORT_SYMBOL(__wrap_devm_memremap);
Dan Williams6bc75612015-06-17 17:23:32 -040096
Ross Zwisler67a3e8f2015-08-27 13:14:20 -060097void *__wrap_memremap(resource_size_t offset, size_t size,
98 unsigned long flags)
99{
100 struct nfit_test_resource *nfit_res;
101
102 rcu_read_lock();
103 nfit_res = get_nfit_res(offset);
104 rcu_read_unlock();
105 if (nfit_res)
106 return nfit_res->buf + offset - nfit_res->res->start;
107 return memremap(offset, size, flags);
108}
109EXPORT_SYMBOL(__wrap_memremap);
110
Dan Williams6bc75612015-06-17 17:23:32 -0400111void __iomem *__wrap_ioremap_nocache(resource_size_t offset, unsigned long size)
112{
113 return __nfit_test_ioremap(offset, size, ioremap_nocache);
114}
115EXPORT_SYMBOL(__wrap_ioremap_nocache);
116
Dan Williams9d27a872015-07-10 14:07:03 -0400117void __iomem *__wrap_ioremap_wc(resource_size_t offset, unsigned long size)
118{
119 return __nfit_test_ioremap(offset, size, ioremap_wc);
120}
121EXPORT_SYMBOL(__wrap_ioremap_wc);
122
Dan Williams6bc75612015-06-17 17:23:32 -0400123void __wrap_iounmap(volatile void __iomem *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 iounmap(addr);
133}
134EXPORT_SYMBOL(__wrap_iounmap);
135
Ross Zwisler67a3e8f2015-08-27 13:14:20 -0600136void __wrap_memunmap(void *addr)
137{
138 struct nfit_test_resource *nfit_res;
139
140 rcu_read_lock();
141 nfit_res = get_nfit_res((unsigned long) addr);
142 rcu_read_unlock();
143 if (nfit_res)
144 return;
145 return memunmap(addr);
146}
147EXPORT_SYMBOL(__wrap_memunmap);
148
Christoph Hellwig708ab622015-08-10 23:07:08 -0400149static struct resource *nfit_test_request_region(struct device *dev,
150 struct resource *parent, resource_size_t start,
151 resource_size_t n, const char *name, int flags)
Dan Williams6bc75612015-06-17 17:23:32 -0400152{
153 struct nfit_test_resource *nfit_res;
154
155 if (parent == &iomem_resource) {
156 rcu_read_lock();
157 nfit_res = get_nfit_res(start);
158 rcu_read_unlock();
159 if (nfit_res) {
160 struct resource *res = nfit_res->res + 1;
161
162 if (start + n > nfit_res->res->start
163 + resource_size(nfit_res->res)) {
164 pr_debug("%s: start: %llx n: %llx overflow: %pr\n",
165 __func__, start, n,
166 nfit_res->res);
167 return NULL;
168 }
169
170 res->start = start;
171 res->end = start + n - 1;
172 res->name = name;
173 res->flags = resource_type(parent);
174 res->flags |= IORESOURCE_BUSY | flags;
175 pr_debug("%s: %pr\n", __func__, res);
176 return res;
177 }
178 }
Christoph Hellwig708ab622015-08-10 23:07:08 -0400179 if (dev)
180 return __devm_request_region(dev, parent, start, n, name);
Dan Williams6bc75612015-06-17 17:23:32 -0400181 return __request_region(parent, start, n, name, flags);
182}
Christoph Hellwig708ab622015-08-10 23:07:08 -0400183
184struct resource *__wrap___request_region(struct resource *parent,
185 resource_size_t start, resource_size_t n, const char *name,
186 int flags)
187{
188 return nfit_test_request_region(NULL, parent, start, n, name, flags);
189}
Dan Williams6bc75612015-06-17 17:23:32 -0400190EXPORT_SYMBOL(__wrap___request_region);
191
Christoph Hellwig708ab622015-08-10 23:07:08 -0400192struct resource *__wrap___devm_request_region(struct device *dev,
193 struct resource *parent, resource_size_t start,
194 resource_size_t n, const char *name)
195{
196 if (!dev)
197 return NULL;
198 return nfit_test_request_region(dev, parent, start, n, name, 0);
199}
200EXPORT_SYMBOL(__wrap___devm_request_region);
201
Dan Williams6bc75612015-06-17 17:23:32 -0400202void __wrap___release_region(struct resource *parent, resource_size_t start,
203 resource_size_t n)
204{
205 struct nfit_test_resource *nfit_res;
206
207 if (parent == &iomem_resource) {
208 rcu_read_lock();
209 nfit_res = get_nfit_res(start);
210 rcu_read_unlock();
211 if (nfit_res) {
212 struct resource *res = nfit_res->res + 1;
213
214 if (start != res->start || resource_size(res) != n)
215 pr_info("%s: start: %llx n: %llx mismatch: %pr\n",
216 __func__, start, n, res);
217 else
218 memset(res, 0, sizeof(*res));
219 return;
220 }
221 }
222 __release_region(parent, start, n);
223}
224EXPORT_SYMBOL(__wrap___release_region);
225
226MODULE_LICENSE("GPL v2");