blob: b752ce47ead3c9599c01f5c476c26ac8431a42b6 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williams6bc75612015-06-17 17:23:32 -04002/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
Dan Williams6bc75612015-06-17 17:23:32 -04004 */
Dan Williamsb28f08c2016-06-15 20:17:02 -07005#include <linux/memremap.h>
Dan Williams6bc75612015-06-17 17:23:32 -04006#include <linux/rculist.h>
7#include <linux/export.h>
8#include <linux/ioport.h>
9#include <linux/module.h>
10#include <linux/types.h>
Dan Williamsb28f08c2016-06-15 20:17:02 -070011#include <linux/pfn_t.h>
Dan Williamsc14a8682016-08-18 22:15:04 -070012#include <linux/acpi.h>
Dan Williams6bc75612015-06-17 17:23:32 -040013#include <linux/io.h>
Dan Williams9bfa8492015-12-11 11:20:16 -080014#include <linux/mm.h>
Dan Williams6bc75612015-06-17 17:23:32 -040015#include "nfit_test.h"
16
17static LIST_HEAD(iomap_head);
18
19static struct iomap_ops {
20 nfit_test_lookup_fn nfit_test_lookup;
Dan Williamsa7de92d2016-12-05 13:43:25 -080021 nfit_test_evaluate_dsm_fn evaluate_dsm;
Dan Williams6bc75612015-06-17 17:23:32 -040022 struct list_head list;
23} iomap_ops = {
24 .list = LIST_HEAD_INIT(iomap_ops.list),
25};
26
Dan Williamsa7de92d2016-12-05 13:43:25 -080027void nfit_test_setup(nfit_test_lookup_fn lookup,
28 nfit_test_evaluate_dsm_fn evaluate)
Dan Williams6bc75612015-06-17 17:23:32 -040029{
30 iomap_ops.nfit_test_lookup = lookup;
Dan Williamsa7de92d2016-12-05 13:43:25 -080031 iomap_ops.evaluate_dsm = evaluate;
Dan Williams6bc75612015-06-17 17:23:32 -040032 list_add_rcu(&iomap_ops.list, &iomap_head);
33}
34EXPORT_SYMBOL(nfit_test_setup);
35
36void nfit_test_teardown(void)
37{
38 list_del_rcu(&iomap_ops.list);
39 synchronize_rcu();
40}
41EXPORT_SYMBOL(nfit_test_teardown);
42
Dan Williams9bfa8492015-12-11 11:20:16 -080043static struct nfit_test_resource *__get_nfit_res(resource_size_t resource)
Dan Williams6bc75612015-06-17 17:23:32 -040044{
45 struct iomap_ops *ops;
46
47 ops = list_first_or_null_rcu(&iomap_head, typeof(*ops), list);
48 if (ops)
49 return ops->nfit_test_lookup(resource);
50 return NULL;
51}
52
Dan Williamsf295e532016-06-17 11:08:06 -070053struct nfit_test_resource *get_nfit_res(resource_size_t resource)
Dan Williams9bfa8492015-12-11 11:20:16 -080054{
55 struct nfit_test_resource *res;
56
57 rcu_read_lock();
58 res = __get_nfit_res(resource);
59 rcu_read_unlock();
60
61 return res;
62}
Dan Williamsf295e532016-06-17 11:08:06 -070063EXPORT_SYMBOL(get_nfit_res);
Dan Williams9bfa8492015-12-11 11:20:16 -080064
Zou Wei7ddb4cc2021-04-20 15:47:47 +080065static void __iomem *__nfit_test_ioremap(resource_size_t offset, unsigned long size,
Dan Williams6bc75612015-06-17 17:23:32 -040066 void __iomem *(*fallback_fn)(resource_size_t, unsigned long))
67{
Dan Williams9bfa8492015-12-11 11:20:16 -080068 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
Dan Williams6bc75612015-06-17 17:23:32 -040069
Dan Williams6bc75612015-06-17 17:23:32 -040070 if (nfit_res)
71 return (void __iomem *) nfit_res->buf + offset
Dan Williamsbd4cd742016-10-06 11:22:37 -070072 - nfit_res->res.start;
Dan Williams6bc75612015-06-17 17:23:32 -040073 return fallback_fn(offset, size);
74}
75
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +010076void __iomem *__wrap_devm_ioremap(struct device *dev,
Dan Williams9d27a872015-07-10 14:07:03 -040077 resource_size_t offset, unsigned long size)
78{
Dan Williams9bfa8492015-12-11 11:20:16 -080079 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
Dan Williams9d27a872015-07-10 14:07:03 -040080
Dan Williams9d27a872015-07-10 14:07:03 -040081 if (nfit_res)
82 return (void __iomem *) nfit_res->buf + offset
Dan Williamsbd4cd742016-10-06 11:22:37 -070083 - nfit_res->res.start;
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +010084 return devm_ioremap(dev, offset, size);
Dan Williams9d27a872015-07-10 14:07:03 -040085}
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +010086EXPORT_SYMBOL(__wrap_devm_ioremap);
Dan Williams9d27a872015-07-10 14:07:03 -040087
Christoph Hellwig708ab622015-08-10 23:07:08 -040088void *__wrap_devm_memremap(struct device *dev, resource_size_t offset,
89 size_t size, unsigned long flags)
Dan Williams6bc75612015-06-17 17:23:32 -040090{
Dan Williams9bfa8492015-12-11 11:20:16 -080091 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
Dan Williamse836a252015-08-12 18:42:56 -040092
Dan Williamse836a252015-08-12 18:42:56 -040093 if (nfit_res)
Dan Williamsbd4cd742016-10-06 11:22:37 -070094 return nfit_res->buf + offset - nfit_res->res.start;
Christoph Hellwig708ab622015-08-10 23:07:08 -040095 return devm_memremap(dev, offset, size, flags);
Dan Williams6bc75612015-06-17 17:23:32 -040096}
Christoph Hellwig708ab622015-08-10 23:07:08 -040097EXPORT_SYMBOL(__wrap_devm_memremap);
Dan Williams6bc75612015-06-17 17:23:32 -040098
Dan Williamsa95c90f2018-12-28 00:34:57 -080099static void nfit_test_kill(void *_pgmap)
100{
101 struct dev_pagemap *pgmap = _pgmap;
102
Christoph Hellwigb80892c2021-10-28 17:10:17 +0200103 WARN_ON(!pgmap);
Christoph Hellwig24917f62019-06-26 14:27:14 +0200104
Christoph Hellwigb80892c2021-10-28 17:10:17 +0200105 percpu_ref_kill(&pgmap->ref);
Christoph Hellwig24917f62019-06-26 14:27:14 +0200106
Christoph Hellwigb80892c2021-10-28 17:10:17 +0200107 wait_for_completion(&pgmap->done);
108 percpu_ref_exit(&pgmap->ref);
Christoph Hellwig24917f62019-06-26 14:27:14 +0200109}
110
111static void dev_pagemap_percpu_release(struct percpu_ref *ref)
112{
Christoph Hellwigb80892c2021-10-28 17:10:17 +0200113 struct dev_pagemap *pgmap = container_of(ref, struct dev_pagemap, ref);
Christoph Hellwig24917f62019-06-26 14:27:14 +0200114
115 complete(&pgmap->done);
Dan Williamsa95c90f2018-12-28 00:34:57 -0800116}
117
Christoph Hellwige8d51342017-12-29 08:54:05 +0100118void *__wrap_devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
Dan Williams979fccf2015-12-15 00:34:21 -0800119{
Christoph Hellwig24917f62019-06-26 14:27:14 +0200120 int error;
Dan Williamsa4574f62020-10-13 16:50:29 -0700121 resource_size_t offset = pgmap->range.start;
Dan Williams9bfa8492015-12-11 11:20:16 -0800122 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
Dan Williams979fccf2015-12-15 00:34:21 -0800123
Christoph Hellwig24917f62019-06-26 14:27:14 +0200124 if (!nfit_res)
125 return devm_memremap_pages(dev, pgmap);
Dan Williamsa95c90f2018-12-28 00:34:57 -0800126
Christoph Hellwigb80892c2021-10-28 17:10:17 +0200127 init_completion(&pgmap->done);
128 error = percpu_ref_init(&pgmap->ref, dev_pagemap_percpu_release, 0,
129 GFP_KERNEL);
130 if (error)
131 return ERR_PTR(error);
Christoph Hellwig24917f62019-06-26 14:27:14 +0200132
133 error = devm_add_action_or_reset(dev, nfit_test_kill, pgmap);
134 if (error)
135 return ERR_PTR(error);
136 return nfit_res->buf + offset - nfit_res->res.start;
Dan Williams979fccf2015-12-15 00:34:21 -0800137}
Dan Williams808153e2018-12-28 00:34:50 -0800138EXPORT_SYMBOL_GPL(__wrap_devm_memremap_pages);
Dan Williams979fccf2015-12-15 00:34:21 -0800139
Dan Williams76e9f0e2016-01-22 09:43:28 -0800140pfn_t __wrap_phys_to_pfn_t(phys_addr_t addr, unsigned long flags)
Dan Williams979fccf2015-12-15 00:34:21 -0800141{
Dan Williams9bfa8492015-12-11 11:20:16 -0800142 struct nfit_test_resource *nfit_res = get_nfit_res(addr);
Dan Williams979fccf2015-12-15 00:34:21 -0800143
Dan Williams979fccf2015-12-15 00:34:21 -0800144 if (nfit_res)
145 flags &= ~PFN_MAP;
146 return phys_to_pfn_t(addr, flags);
147}
148EXPORT_SYMBOL(__wrap_phys_to_pfn_t);
Dan Williams979fccf2015-12-15 00:34:21 -0800149
Ross Zwisler67a3e8f2015-08-27 13:14:20 -0600150void *__wrap_memremap(resource_size_t offset, size_t size,
151 unsigned long flags)
152{
Dan Williams9bfa8492015-12-11 11:20:16 -0800153 struct nfit_test_resource *nfit_res = get_nfit_res(offset);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -0600154
Ross Zwisler67a3e8f2015-08-27 13:14:20 -0600155 if (nfit_res)
Dan Williamsbd4cd742016-10-06 11:22:37 -0700156 return nfit_res->buf + offset - nfit_res->res.start;
Ross Zwisler67a3e8f2015-08-27 13:14:20 -0600157 return memremap(offset, size, flags);
158}
159EXPORT_SYMBOL(__wrap_memremap);
160
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400161void __wrap_devm_memunmap(struct device *dev, void *addr)
162{
Dan Williams9bfa8492015-12-11 11:20:16 -0800163 struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400164
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400165 if (nfit_res)
166 return;
167 return devm_memunmap(dev, addr);
168}
169EXPORT_SYMBOL(__wrap_devm_memunmap);
170
Dan Williamsc1468552019-11-13 16:22:06 -0800171void __iomem *__wrap_ioremap(resource_size_t offset, unsigned long size)
172{
173 return __nfit_test_ioremap(offset, size, ioremap);
174}
175EXPORT_SYMBOL(__wrap_ioremap);
176
Dan Williams9d27a872015-07-10 14:07:03 -0400177void __iomem *__wrap_ioremap_wc(resource_size_t offset, unsigned long size)
178{
179 return __nfit_test_ioremap(offset, size, ioremap_wc);
180}
181EXPORT_SYMBOL(__wrap_ioremap_wc);
182
Dan Williams6bc75612015-06-17 17:23:32 -0400183void __wrap_iounmap(volatile void __iomem *addr)
184{
Dan Williams9bfa8492015-12-11 11:20:16 -0800185 struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
Dan Williams6bc75612015-06-17 17:23:32 -0400186 if (nfit_res)
187 return;
188 return iounmap(addr);
189}
190EXPORT_SYMBOL(__wrap_iounmap);
191
Ross Zwisler67a3e8f2015-08-27 13:14:20 -0600192void __wrap_memunmap(void *addr)
193{
Dan Williams9bfa8492015-12-11 11:20:16 -0800194 struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -0600195
Ross Zwisler67a3e8f2015-08-27 13:14:20 -0600196 if (nfit_res)
197 return;
198 return memunmap(addr);
199}
200EXPORT_SYMBOL(__wrap_memunmap);
201
Dan Williamsbd4cd742016-10-06 11:22:37 -0700202static bool nfit_test_release_region(struct device *dev,
203 struct resource *parent, resource_size_t start,
204 resource_size_t n);
205
206static void nfit_devres_release(struct device *dev, void *data)
207{
208 struct resource *res = *((struct resource **) data);
209
210 WARN_ON(!nfit_test_release_region(NULL, &iomem_resource, res->start,
211 resource_size(res)));
212}
213
214static int match(struct device *dev, void *__res, void *match_data)
215{
216 struct resource *res = *((struct resource **) __res);
217 resource_size_t start = *((resource_size_t *) match_data);
218
219 return res->start == start;
220}
221
222static bool nfit_test_release_region(struct device *dev,
223 struct resource *parent, resource_size_t start,
224 resource_size_t n)
225{
226 if (parent == &iomem_resource) {
227 struct nfit_test_resource *nfit_res = get_nfit_res(start);
228
229 if (nfit_res) {
230 struct nfit_test_request *req;
231 struct resource *res = NULL;
232
233 if (dev) {
234 devres_release(dev, nfit_devres_release, match,
235 &start);
236 return true;
237 }
238
239 spin_lock(&nfit_res->lock);
240 list_for_each_entry(req, &nfit_res->requests, list)
241 if (req->res.start == start) {
242 res = &req->res;
243 list_del(&req->list);
244 break;
245 }
246 spin_unlock(&nfit_res->lock);
247
248 WARN(!res || resource_size(res) != n,
249 "%s: start: %llx n: %llx mismatch: %pr\n",
250 __func__, start, n, res);
251 if (res)
252 kfree(req);
253 return true;
254 }
255 }
256 return false;
257}
258
Christoph Hellwig708ab622015-08-10 23:07:08 -0400259static struct resource *nfit_test_request_region(struct device *dev,
260 struct resource *parent, resource_size_t start,
261 resource_size_t n, const char *name, int flags)
Dan Williams6bc75612015-06-17 17:23:32 -0400262{
263 struct nfit_test_resource *nfit_res;
264
265 if (parent == &iomem_resource) {
Dan Williams6bc75612015-06-17 17:23:32 -0400266 nfit_res = get_nfit_res(start);
Dan Williams6bc75612015-06-17 17:23:32 -0400267 if (nfit_res) {
Dan Williamsbd4cd742016-10-06 11:22:37 -0700268 struct nfit_test_request *req;
269 struct resource *res = NULL;
Dan Williams6bc75612015-06-17 17:23:32 -0400270
Dan Williamsbd4cd742016-10-06 11:22:37 -0700271 if (start + n > nfit_res->res.start
272 + resource_size(&nfit_res->res)) {
Dan Williams6bc75612015-06-17 17:23:32 -0400273 pr_debug("%s: start: %llx n: %llx overflow: %pr\n",
274 __func__, start, n,
Dan Williamsbd4cd742016-10-06 11:22:37 -0700275 &nfit_res->res);
Dan Williams6bc75612015-06-17 17:23:32 -0400276 return NULL;
277 }
278
Dan Williamsbd4cd742016-10-06 11:22:37 -0700279 spin_lock(&nfit_res->lock);
280 list_for_each_entry(req, &nfit_res->requests, list)
281 if (start == req->res.start) {
282 res = &req->res;
283 break;
284 }
285 spin_unlock(&nfit_res->lock);
286
287 if (res) {
288 WARN(1, "%pr already busy\n", res);
289 return NULL;
290 }
291
292 req = kzalloc(sizeof(*req), GFP_KERNEL);
293 if (!req)
294 return NULL;
295 INIT_LIST_HEAD(&req->list);
296 res = &req->res;
297
Dan Williams6bc75612015-06-17 17:23:32 -0400298 res->start = start;
299 res->end = start + n - 1;
300 res->name = name;
301 res->flags = resource_type(parent);
302 res->flags |= IORESOURCE_BUSY | flags;
Dan Williamsbd4cd742016-10-06 11:22:37 -0700303 spin_lock(&nfit_res->lock);
304 list_add(&req->list, &nfit_res->requests);
305 spin_unlock(&nfit_res->lock);
306
307 if (dev) {
308 struct resource **d;
309
310 d = devres_alloc(nfit_devres_release,
311 sizeof(struct resource *),
312 GFP_KERNEL);
313 if (!d)
314 return NULL;
315 *d = res;
316 devres_add(dev, d);
317 }
318
Dan Williams6bc75612015-06-17 17:23:32 -0400319 pr_debug("%s: %pr\n", __func__, res);
320 return res;
321 }
322 }
Christoph Hellwig708ab622015-08-10 23:07:08 -0400323 if (dev)
324 return __devm_request_region(dev, parent, start, n, name);
Dan Williams6bc75612015-06-17 17:23:32 -0400325 return __request_region(parent, start, n, name, flags);
326}
Christoph Hellwig708ab622015-08-10 23:07:08 -0400327
328struct resource *__wrap___request_region(struct resource *parent,
329 resource_size_t start, resource_size_t n, const char *name,
330 int flags)
331{
332 return nfit_test_request_region(NULL, parent, start, n, name, flags);
333}
Dan Williams6bc75612015-06-17 17:23:32 -0400334EXPORT_SYMBOL(__wrap___request_region);
335
Dan Williamsee8520fe2016-06-15 20:34:17 -0700336int __wrap_insert_resource(struct resource *parent, struct resource *res)
337{
338 if (get_nfit_res(res->start))
339 return 0;
340 return insert_resource(parent, res);
341}
342EXPORT_SYMBOL(__wrap_insert_resource);
343
344int __wrap_remove_resource(struct resource *res)
345{
346 if (get_nfit_res(res->start))
347 return 0;
348 return remove_resource(res);
349}
350EXPORT_SYMBOL(__wrap_remove_resource);
351
Christoph Hellwig708ab622015-08-10 23:07:08 -0400352struct resource *__wrap___devm_request_region(struct device *dev,
353 struct resource *parent, resource_size_t start,
354 resource_size_t n, const char *name)
355{
356 if (!dev)
357 return NULL;
358 return nfit_test_request_region(dev, parent, start, n, name, 0);
359}
360EXPORT_SYMBOL(__wrap___devm_request_region);
361
Dan Williams200c79d2016-03-22 00:22:16 -0700362void __wrap___release_region(struct resource *parent, resource_size_t start,
363 resource_size_t n)
364{
Dan Williamsbd4cd742016-10-06 11:22:37 -0700365 if (!nfit_test_release_region(NULL, parent, start, n))
Dan Williams200c79d2016-03-22 00:22:16 -0700366 __release_region(parent, start, n);
Dan Williams6bc75612015-06-17 17:23:32 -0400367}
368EXPORT_SYMBOL(__wrap___release_region);
369
Dan Williams200c79d2016-03-22 00:22:16 -0700370void __wrap___devm_release_region(struct device *dev, struct resource *parent,
371 resource_size_t start, resource_size_t n)
372{
Dan Williamsbd4cd742016-10-06 11:22:37 -0700373 if (!nfit_test_release_region(dev, parent, start, n))
Dan Williams200c79d2016-03-22 00:22:16 -0700374 __devm_release_region(dev, parent, start, n);
375}
376EXPORT_SYMBOL(__wrap___devm_release_region);
377
Dan Williamsc14a8682016-08-18 22:15:04 -0700378acpi_status __wrap_acpi_evaluate_object(acpi_handle handle, acpi_string path,
379 struct acpi_object_list *p, struct acpi_buffer *buf)
380{
381 struct nfit_test_resource *nfit_res = get_nfit_res((long) handle);
382 union acpi_object **obj;
383
384 if (!nfit_res || strcmp(path, "_FIT") || !buf)
385 return acpi_evaluate_object(handle, path, p, buf);
386
387 obj = nfit_res->buf;
388 buf->length = sizeof(union acpi_object);
389 buf->pointer = *obj;
390 return AE_OK;
391}
392EXPORT_SYMBOL(__wrap_acpi_evaluate_object);
393
Andy Shevchenko94116f82017-06-05 19:40:46 +0300394union acpi_object * __wrap_acpi_evaluate_dsm(acpi_handle handle, const guid_t *guid,
Dan Williamsa7de92d2016-12-05 13:43:25 -0800395 u64 rev, u64 func, union acpi_object *argv4)
396{
397 union acpi_object *obj = ERR_PTR(-ENXIO);
398 struct iomap_ops *ops;
399
400 rcu_read_lock();
401 ops = list_first_or_null_rcu(&iomap_head, typeof(*ops), list);
402 if (ops)
Andy Shevchenko94116f82017-06-05 19:40:46 +0300403 obj = ops->evaluate_dsm(handle, guid, rev, func, argv4);
Dan Williamsa7de92d2016-12-05 13:43:25 -0800404 rcu_read_unlock();
405
406 if (IS_ERR(obj))
Andy Shevchenko94116f82017-06-05 19:40:46 +0300407 return acpi_evaluate_dsm(handle, guid, rev, func, argv4);
Dan Williamsa7de92d2016-12-05 13:43:25 -0800408 return obj;
409}
410EXPORT_SYMBOL(__wrap_acpi_evaluate_dsm);
411
Dan Williams6bc75612015-06-17 17:23:32 -0400412MODULE_LICENSE("GPL v2");