Merge remote-tracking branches 'regulator/fix/88pm800', 'regulator/fix/max8973',...
[linux-drm-fsl-dcu.git] / tools / testing / nvdimm / test / iomap.c
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
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
83 void __iomem *__wrap_ioremap_cache(resource_size_t offset, unsigned long size)
84 {
85         return __nfit_test_ioremap(offset, size, ioremap_cache);
86 }
87 EXPORT_SYMBOL(__wrap_ioremap_cache);
88
89 void __iomem *__wrap_ioremap_nocache(resource_size_t offset, unsigned long size)
90 {
91         return __nfit_test_ioremap(offset, size, ioremap_nocache);
92 }
93 EXPORT_SYMBOL(__wrap_ioremap_nocache);
94
95 void __iomem *__wrap_ioremap_wt(resource_size_t offset, unsigned long size)
96 {
97         return __nfit_test_ioremap(offset, size, ioremap_wt);
98 }
99 EXPORT_SYMBOL(__wrap_ioremap_wt);
100
101 void __iomem *__wrap_ioremap_wc(resource_size_t offset, unsigned long size)
102 {
103         return __nfit_test_ioremap(offset, size, ioremap_wc);
104 }
105 EXPORT_SYMBOL(__wrap_ioremap_wc);
106
107 void __wrap_iounmap(volatile void __iomem *addr)
108 {
109         struct nfit_test_resource *nfit_res;
110
111         rcu_read_lock();
112         nfit_res = get_nfit_res((unsigned long) addr);
113         rcu_read_unlock();
114         if (nfit_res)
115                 return;
116         return iounmap(addr);
117 }
118 EXPORT_SYMBOL(__wrap_iounmap);
119
120 struct resource *__wrap___request_region(struct resource *parent,
121                 resource_size_t start, resource_size_t n, const char *name,
122                 int flags)
123 {
124         struct nfit_test_resource *nfit_res;
125
126         if (parent == &iomem_resource) {
127                 rcu_read_lock();
128                 nfit_res = get_nfit_res(start);
129                 rcu_read_unlock();
130                 if (nfit_res) {
131                         struct resource *res = nfit_res->res + 1;
132
133                         if (start + n > nfit_res->res->start
134                                         + resource_size(nfit_res->res)) {
135                                 pr_debug("%s: start: %llx n: %llx overflow: %pr\n",
136                                                 __func__, start, n,
137                                                 nfit_res->res);
138                                 return NULL;
139                         }
140
141                         res->start = start;
142                         res->end = start + n - 1;
143                         res->name = name;
144                         res->flags = resource_type(parent);
145                         res->flags |= IORESOURCE_BUSY | flags;
146                         pr_debug("%s: %pr\n", __func__, res);
147                         return res;
148                 }
149         }
150         return __request_region(parent, start, n, name, flags);
151 }
152 EXPORT_SYMBOL(__wrap___request_region);
153
154 void __wrap___release_region(struct resource *parent, resource_size_t start,
155                                 resource_size_t n)
156 {
157         struct nfit_test_resource *nfit_res;
158
159         if (parent == &iomem_resource) {
160                 rcu_read_lock();
161                 nfit_res = get_nfit_res(start);
162                 rcu_read_unlock();
163                 if (nfit_res) {
164                         struct resource *res = nfit_res->res + 1;
165
166                         if (start != res->start || resource_size(res) != n)
167                                 pr_info("%s: start: %llx n: %llx mismatch: %pr\n",
168                                                 __func__, start, n, res);
169                         else
170                                 memset(res, 0, sizeof(*res));
171                         return;
172                 }
173         }
174         __release_region(parent, start, n);
175 }
176 EXPORT_SYMBOL(__wrap___release_region);
177
178 MODULE_LICENSE("GPL v2");