iomap.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/memremap.h>
  14. #include <linux/rculist.h>
  15. #include <linux/export.h>
  16. #include <linux/ioport.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/pfn_t.h>
  20. #include <linux/acpi.h>
  21. #include <linux/io.h>
  22. #include <linux/mm.h>
  23. #include "nfit_test.h"
  24. static LIST_HEAD(iomap_head);
  25. static struct iomap_ops {
  26. nfit_test_lookup_fn nfit_test_lookup;
  27. nfit_test_evaluate_dsm_fn evaluate_dsm;
  28. struct list_head list;
  29. } iomap_ops = {
  30. .list = LIST_HEAD_INIT(iomap_ops.list),
  31. };
  32. void nfit_test_setup(nfit_test_lookup_fn lookup,
  33. nfit_test_evaluate_dsm_fn evaluate)
  34. {
  35. iomap_ops.nfit_test_lookup = lookup;
  36. iomap_ops.evaluate_dsm = evaluate;
  37. list_add_rcu(&iomap_ops.list, &iomap_head);
  38. }
  39. EXPORT_SYMBOL(nfit_test_setup);
  40. void nfit_test_teardown(void)
  41. {
  42. list_del_rcu(&iomap_ops.list);
  43. synchronize_rcu();
  44. }
  45. EXPORT_SYMBOL(nfit_test_teardown);
  46. static struct nfit_test_resource *__get_nfit_res(resource_size_t resource)
  47. {
  48. struct iomap_ops *ops;
  49. ops = list_first_or_null_rcu(&iomap_head, typeof(*ops), list);
  50. if (ops)
  51. return ops->nfit_test_lookup(resource);
  52. return NULL;
  53. }
  54. struct nfit_test_resource *get_nfit_res(resource_size_t resource)
  55. {
  56. struct nfit_test_resource *res;
  57. rcu_read_lock();
  58. res = __get_nfit_res(resource);
  59. rcu_read_unlock();
  60. return res;
  61. }
  62. EXPORT_SYMBOL(get_nfit_res);
  63. void __iomem *__nfit_test_ioremap(resource_size_t offset, unsigned long size,
  64. void __iomem *(*fallback_fn)(resource_size_t, unsigned long))
  65. {
  66. struct nfit_test_resource *nfit_res = get_nfit_res(offset);
  67. if (nfit_res)
  68. return (void __iomem *) nfit_res->buf + offset
  69. - nfit_res->res.start;
  70. return fallback_fn(offset, size);
  71. }
  72. void __iomem *__wrap_devm_ioremap_nocache(struct device *dev,
  73. resource_size_t offset, unsigned long size)
  74. {
  75. struct nfit_test_resource *nfit_res = get_nfit_res(offset);
  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. void *__wrap_devm_memremap(struct device *dev, resource_size_t offset,
  83. size_t size, unsigned long flags)
  84. {
  85. struct nfit_test_resource *nfit_res = get_nfit_res(offset);
  86. if (nfit_res)
  87. return nfit_res->buf + offset - nfit_res->res.start;
  88. return devm_memremap(dev, offset, size, flags);
  89. }
  90. EXPORT_SYMBOL(__wrap_devm_memremap);
  91. void *__wrap_devm_memremap_pages(struct device *dev, struct resource *res,
  92. struct percpu_ref *ref, struct vmem_altmap *altmap)
  93. {
  94. resource_size_t offset = res->start;
  95. struct nfit_test_resource *nfit_res = get_nfit_res(offset);
  96. if (nfit_res)
  97. return nfit_res->buf + offset - nfit_res->res.start;
  98. return devm_memremap_pages(dev, res, ref, altmap);
  99. }
  100. EXPORT_SYMBOL(__wrap_devm_memremap_pages);
  101. pfn_t __wrap_phys_to_pfn_t(phys_addr_t addr, unsigned long flags)
  102. {
  103. struct nfit_test_resource *nfit_res = get_nfit_res(addr);
  104. if (nfit_res)
  105. flags &= ~PFN_MAP;
  106. return phys_to_pfn_t(addr, flags);
  107. }
  108. EXPORT_SYMBOL(__wrap_phys_to_pfn_t);
  109. void *__wrap_memremap(resource_size_t offset, size_t size,
  110. unsigned long flags)
  111. {
  112. struct nfit_test_resource *nfit_res = get_nfit_res(offset);
  113. if (nfit_res)
  114. return nfit_res->buf + offset - nfit_res->res.start;
  115. return memremap(offset, size, flags);
  116. }
  117. EXPORT_SYMBOL(__wrap_memremap);
  118. void __wrap_devm_memunmap(struct device *dev, void *addr)
  119. {
  120. struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
  121. if (nfit_res)
  122. return;
  123. return devm_memunmap(dev, addr);
  124. }
  125. EXPORT_SYMBOL(__wrap_devm_memunmap);
  126. void __iomem *__wrap_ioremap_nocache(resource_size_t offset, unsigned long size)
  127. {
  128. return __nfit_test_ioremap(offset, size, ioremap_nocache);
  129. }
  130. EXPORT_SYMBOL(__wrap_ioremap_nocache);
  131. void __iomem *__wrap_ioremap_wc(resource_size_t offset, unsigned long size)
  132. {
  133. return __nfit_test_ioremap(offset, size, ioremap_wc);
  134. }
  135. EXPORT_SYMBOL(__wrap_ioremap_wc);
  136. void __wrap_iounmap(volatile void __iomem *addr)
  137. {
  138. struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
  139. if (nfit_res)
  140. return;
  141. return iounmap(addr);
  142. }
  143. EXPORT_SYMBOL(__wrap_iounmap);
  144. void __wrap_memunmap(void *addr)
  145. {
  146. struct nfit_test_resource *nfit_res = get_nfit_res((long) addr);
  147. if (nfit_res)
  148. return;
  149. return memunmap(addr);
  150. }
  151. EXPORT_SYMBOL(__wrap_memunmap);
  152. static bool nfit_test_release_region(struct device *dev,
  153. struct resource *parent, resource_size_t start,
  154. resource_size_t n);
  155. static void nfit_devres_release(struct device *dev, void *data)
  156. {
  157. struct resource *res = *((struct resource **) data);
  158. WARN_ON(!nfit_test_release_region(NULL, &iomem_resource, res->start,
  159. resource_size(res)));
  160. }
  161. static int match(struct device *dev, void *__res, void *match_data)
  162. {
  163. struct resource *res = *((struct resource **) __res);
  164. resource_size_t start = *((resource_size_t *) match_data);
  165. return res->start == start;
  166. }
  167. static bool nfit_test_release_region(struct device *dev,
  168. struct resource *parent, resource_size_t start,
  169. resource_size_t n)
  170. {
  171. if (parent == &iomem_resource) {
  172. struct nfit_test_resource *nfit_res = get_nfit_res(start);
  173. if (nfit_res) {
  174. struct nfit_test_request *req;
  175. struct resource *res = NULL;
  176. if (dev) {
  177. devres_release(dev, nfit_devres_release, match,
  178. &start);
  179. return true;
  180. }
  181. spin_lock(&nfit_res->lock);
  182. list_for_each_entry(req, &nfit_res->requests, list)
  183. if (req->res.start == start) {
  184. res = &req->res;
  185. list_del(&req->list);
  186. break;
  187. }
  188. spin_unlock(&nfit_res->lock);
  189. WARN(!res || resource_size(res) != n,
  190. "%s: start: %llx n: %llx mismatch: %pr\n",
  191. __func__, start, n, res);
  192. if (res)
  193. kfree(req);
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. static struct resource *nfit_test_request_region(struct device *dev,
  200. struct resource *parent, resource_size_t start,
  201. resource_size_t n, const char *name, int flags)
  202. {
  203. struct nfit_test_resource *nfit_res;
  204. if (parent == &iomem_resource) {
  205. nfit_res = get_nfit_res(start);
  206. if (nfit_res) {
  207. struct nfit_test_request *req;
  208. struct resource *res = NULL;
  209. if (start + n > nfit_res->res.start
  210. + resource_size(&nfit_res->res)) {
  211. pr_debug("%s: start: %llx n: %llx overflow: %pr\n",
  212. __func__, start, n,
  213. &nfit_res->res);
  214. return NULL;
  215. }
  216. spin_lock(&nfit_res->lock);
  217. list_for_each_entry(req, &nfit_res->requests, list)
  218. if (start == req->res.start) {
  219. res = &req->res;
  220. break;
  221. }
  222. spin_unlock(&nfit_res->lock);
  223. if (res) {
  224. WARN(1, "%pr already busy\n", res);
  225. return NULL;
  226. }
  227. req = kzalloc(sizeof(*req), GFP_KERNEL);
  228. if (!req)
  229. return NULL;
  230. INIT_LIST_HEAD(&req->list);
  231. res = &req->res;
  232. res->start = start;
  233. res->end = start + n - 1;
  234. res->name = name;
  235. res->flags = resource_type(parent);
  236. res->flags |= IORESOURCE_BUSY | flags;
  237. spin_lock(&nfit_res->lock);
  238. list_add(&req->list, &nfit_res->requests);
  239. spin_unlock(&nfit_res->lock);
  240. if (dev) {
  241. struct resource **d;
  242. d = devres_alloc(nfit_devres_release,
  243. sizeof(struct resource *),
  244. GFP_KERNEL);
  245. if (!d)
  246. return NULL;
  247. *d = res;
  248. devres_add(dev, d);
  249. }
  250. pr_debug("%s: %pr\n", __func__, res);
  251. return res;
  252. }
  253. }
  254. if (dev)
  255. return __devm_request_region(dev, parent, start, n, name);
  256. return __request_region(parent, start, n, name, flags);
  257. }
  258. struct resource *__wrap___request_region(struct resource *parent,
  259. resource_size_t start, resource_size_t n, const char *name,
  260. int flags)
  261. {
  262. return nfit_test_request_region(NULL, parent, start, n, name, flags);
  263. }
  264. EXPORT_SYMBOL(__wrap___request_region);
  265. int __wrap_insert_resource(struct resource *parent, struct resource *res)
  266. {
  267. if (get_nfit_res(res->start))
  268. return 0;
  269. return insert_resource(parent, res);
  270. }
  271. EXPORT_SYMBOL(__wrap_insert_resource);
  272. int __wrap_remove_resource(struct resource *res)
  273. {
  274. if (get_nfit_res(res->start))
  275. return 0;
  276. return remove_resource(res);
  277. }
  278. EXPORT_SYMBOL(__wrap_remove_resource);
  279. struct resource *__wrap___devm_request_region(struct device *dev,
  280. struct resource *parent, resource_size_t start,
  281. resource_size_t n, const char *name)
  282. {
  283. if (!dev)
  284. return NULL;
  285. return nfit_test_request_region(dev, parent, start, n, name, 0);
  286. }
  287. EXPORT_SYMBOL(__wrap___devm_request_region);
  288. void __wrap___release_region(struct resource *parent, resource_size_t start,
  289. resource_size_t n)
  290. {
  291. if (!nfit_test_release_region(NULL, parent, start, n))
  292. __release_region(parent, start, n);
  293. }
  294. EXPORT_SYMBOL(__wrap___release_region);
  295. void __wrap___devm_release_region(struct device *dev, struct resource *parent,
  296. resource_size_t start, resource_size_t n)
  297. {
  298. if (!nfit_test_release_region(dev, parent, start, n))
  299. __devm_release_region(dev, parent, start, n);
  300. }
  301. EXPORT_SYMBOL(__wrap___devm_release_region);
  302. acpi_status __wrap_acpi_evaluate_object(acpi_handle handle, acpi_string path,
  303. struct acpi_object_list *p, struct acpi_buffer *buf)
  304. {
  305. struct nfit_test_resource *nfit_res = get_nfit_res((long) handle);
  306. union acpi_object **obj;
  307. if (!nfit_res || strcmp(path, "_FIT") || !buf)
  308. return acpi_evaluate_object(handle, path, p, buf);
  309. obj = nfit_res->buf;
  310. buf->length = sizeof(union acpi_object);
  311. buf->pointer = *obj;
  312. return AE_OK;
  313. }
  314. EXPORT_SYMBOL(__wrap_acpi_evaluate_object);
  315. union acpi_object * __wrap_acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid,
  316. u64 rev, u64 func, union acpi_object *argv4)
  317. {
  318. union acpi_object *obj = ERR_PTR(-ENXIO);
  319. struct iomap_ops *ops;
  320. rcu_read_lock();
  321. ops = list_first_or_null_rcu(&iomap_head, typeof(*ops), list);
  322. if (ops)
  323. obj = ops->evaluate_dsm(handle, uuid, rev, func, argv4);
  324. rcu_read_unlock();
  325. if (IS_ERR(obj))
  326. return acpi_evaluate_dsm(handle, uuid, rev, func, argv4);
  327. return obj;
  328. }
  329. EXPORT_SYMBOL(__wrap_acpi_evaluate_dsm);
  330. MODULE_LICENSE("GPL v2");