memremap.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright(c) 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/radix-tree.h>
  14. #include <linux/memremap.h>
  15. #include <linux/device.h>
  16. #include <linux/types.h>
  17. #include <linux/pfn_t.h>
  18. #include <linux/io.h>
  19. #include <linux/mm.h>
  20. #include <linux/memory_hotplug.h>
  21. #ifndef ioremap_cache
  22. /* temporary while we convert existing ioremap_cache users to memremap */
  23. __weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size)
  24. {
  25. return ioremap(offset, size);
  26. }
  27. #endif
  28. static void *try_ram_remap(resource_size_t offset, size_t size)
  29. {
  30. struct page *page = pfn_to_page(offset >> PAGE_SHIFT);
  31. /* In the simple case just return the existing linear address */
  32. if (!PageHighMem(page))
  33. return __va(offset);
  34. return NULL; /* fallback to ioremap_cache */
  35. }
  36. /**
  37. * memremap() - remap an iomem_resource as cacheable memory
  38. * @offset: iomem resource start address
  39. * @size: size of remap
  40. * @flags: either MEMREMAP_WB or MEMREMAP_WT
  41. *
  42. * memremap() is "ioremap" for cases where it is known that the resource
  43. * being mapped does not have i/o side effects and the __iomem
  44. * annotation is not applicable.
  45. *
  46. * MEMREMAP_WB - matches the default mapping for "System RAM" on
  47. * the architecture. This is usually a read-allocate write-back cache.
  48. * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
  49. * memremap() will bypass establishing a new mapping and instead return
  50. * a pointer into the direct map.
  51. *
  52. * MEMREMAP_WT - establish a mapping whereby writes either bypass the
  53. * cache or are written through to memory and never exist in a
  54. * cache-dirty state with respect to program visibility. Attempts to
  55. * map "System RAM" with this mapping type will fail.
  56. */
  57. void *memremap(resource_size_t offset, size_t size, unsigned long flags)
  58. {
  59. int is_ram = region_intersects(offset, size, "System RAM");
  60. void *addr = NULL;
  61. if (is_ram == REGION_MIXED) {
  62. WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
  63. &offset, (unsigned long) size);
  64. return NULL;
  65. }
  66. /* Try all mapping types requested until one returns non-NULL */
  67. if (flags & MEMREMAP_WB) {
  68. flags &= ~MEMREMAP_WB;
  69. /*
  70. * MEMREMAP_WB is special in that it can be satisifed
  71. * from the direct map. Some archs depend on the
  72. * capability of memremap() to autodetect cases where
  73. * the requested range is potentially in "System RAM"
  74. */
  75. if (is_ram == REGION_INTERSECTS)
  76. addr = try_ram_remap(offset, size);
  77. if (!addr)
  78. addr = ioremap_cache(offset, size);
  79. }
  80. /*
  81. * If we don't have a mapping yet and more request flags are
  82. * pending then we will be attempting to establish a new virtual
  83. * address mapping. Enforce that this mapping is not aliasing
  84. * "System RAM"
  85. */
  86. if (!addr && is_ram == REGION_INTERSECTS && flags) {
  87. WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
  88. &offset, (unsigned long) size);
  89. return NULL;
  90. }
  91. if (!addr && (flags & MEMREMAP_WT)) {
  92. flags &= ~MEMREMAP_WT;
  93. addr = ioremap_wt(offset, size);
  94. }
  95. return addr;
  96. }
  97. EXPORT_SYMBOL(memremap);
  98. void memunmap(void *addr)
  99. {
  100. if (is_vmalloc_addr(addr))
  101. iounmap((void __iomem *) addr);
  102. }
  103. EXPORT_SYMBOL(memunmap);
  104. static void devm_memremap_release(struct device *dev, void *res)
  105. {
  106. memunmap(res);
  107. }
  108. static int devm_memremap_match(struct device *dev, void *res, void *match_data)
  109. {
  110. return *(void **)res == match_data;
  111. }
  112. void *devm_memremap(struct device *dev, resource_size_t offset,
  113. size_t size, unsigned long flags)
  114. {
  115. void **ptr, *addr;
  116. ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
  117. dev_to_node(dev));
  118. if (!ptr)
  119. return ERR_PTR(-ENOMEM);
  120. addr = memremap(offset, size, flags);
  121. if (addr) {
  122. *ptr = addr;
  123. devres_add(dev, ptr);
  124. } else
  125. devres_free(ptr);
  126. return addr;
  127. }
  128. EXPORT_SYMBOL(devm_memremap);
  129. void devm_memunmap(struct device *dev, void *addr)
  130. {
  131. WARN_ON(devres_release(dev, devm_memremap_release,
  132. devm_memremap_match, addr));
  133. }
  134. EXPORT_SYMBOL(devm_memunmap);
  135. pfn_t phys_to_pfn_t(dma_addr_t addr, unsigned long flags)
  136. {
  137. return __pfn_to_pfn_t(addr >> PAGE_SHIFT, flags);
  138. }
  139. EXPORT_SYMBOL(phys_to_pfn_t);
  140. #ifdef CONFIG_ZONE_DEVICE
  141. static DEFINE_MUTEX(pgmap_lock);
  142. static RADIX_TREE(pgmap_radix, GFP_KERNEL);
  143. #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
  144. #define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
  145. struct page_map {
  146. struct resource res;
  147. struct percpu_ref *ref;
  148. struct dev_pagemap pgmap;
  149. struct vmem_altmap altmap;
  150. };
  151. static void pgmap_radix_release(struct resource *res)
  152. {
  153. resource_size_t key;
  154. mutex_lock(&pgmap_lock);
  155. for (key = res->start; key <= res->end; key += SECTION_SIZE)
  156. radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT);
  157. mutex_unlock(&pgmap_lock);
  158. }
  159. static void devm_memremap_pages_release(struct device *dev, void *data)
  160. {
  161. struct page_map *page_map = data;
  162. struct resource *res = &page_map->res;
  163. resource_size_t align_start, align_size;
  164. struct dev_pagemap *pgmap = &page_map->pgmap;
  165. pgmap_radix_release(res);
  166. /* pages are dead and unused, undo the arch mapping */
  167. align_start = res->start & ~(SECTION_SIZE - 1);
  168. align_size = ALIGN(resource_size(res), SECTION_SIZE);
  169. arch_remove_memory(align_start, align_size);
  170. dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
  171. "%s: failed to free all reserved pages\n", __func__);
  172. }
  173. /* assumes rcu_read_lock() held at entry */
  174. struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
  175. {
  176. struct page_map *page_map;
  177. WARN_ON_ONCE(!rcu_read_lock_held());
  178. page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
  179. return page_map ? &page_map->pgmap : NULL;
  180. }
  181. /**
  182. * devm_memremap_pages - remap and provide memmap backing for the given resource
  183. * @dev: hosting device for @res
  184. * @res: "host memory" address range
  185. * @altmap: optional descriptor for allocating the memmap from @res
  186. *
  187. * Note, the expectation is that @res is a host memory range that could
  188. * feasibly be treated as a "System RAM" range, i.e. not a device mmio
  189. * range, but this is not enforced.
  190. */
  191. void *devm_memremap_pages(struct device *dev, struct resource *res,
  192. struct vmem_altmap *altmap)
  193. {
  194. int is_ram = region_intersects(res->start, resource_size(res),
  195. "System RAM");
  196. resource_size_t key, align_start, align_size;
  197. struct dev_pagemap *pgmap;
  198. struct page_map *page_map;
  199. int error, nid;
  200. if (is_ram == REGION_MIXED) {
  201. WARN_ONCE(1, "%s attempted on mixed region %pr\n",
  202. __func__, res);
  203. return ERR_PTR(-ENXIO);
  204. }
  205. if (is_ram == REGION_INTERSECTS)
  206. return __va(res->start);
  207. if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) {
  208. dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n",
  209. __func__);
  210. return ERR_PTR(-ENXIO);
  211. }
  212. page_map = devres_alloc_node(devm_memremap_pages_release,
  213. sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
  214. if (!page_map)
  215. return ERR_PTR(-ENOMEM);
  216. pgmap = &page_map->pgmap;
  217. memcpy(&page_map->res, res, sizeof(*res));
  218. pgmap->dev = dev;
  219. if (altmap) {
  220. memcpy(&page_map->altmap, altmap, sizeof(*altmap));
  221. pgmap->altmap = &page_map->altmap;
  222. }
  223. pgmap->res = &page_map->res;
  224. mutex_lock(&pgmap_lock);
  225. error = 0;
  226. for (key = res->start; key <= res->end; key += SECTION_SIZE) {
  227. struct dev_pagemap *dup;
  228. rcu_read_lock();
  229. dup = find_dev_pagemap(key);
  230. rcu_read_unlock();
  231. if (dup) {
  232. dev_err(dev, "%s: %pr collides with mapping for %s\n",
  233. __func__, res, dev_name(dup->dev));
  234. error = -EBUSY;
  235. break;
  236. }
  237. error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT,
  238. page_map);
  239. if (error) {
  240. dev_err(dev, "%s: failed: %d\n", __func__, error);
  241. break;
  242. }
  243. }
  244. mutex_unlock(&pgmap_lock);
  245. if (error)
  246. goto err_radix;
  247. nid = dev_to_node(dev);
  248. if (nid < 0)
  249. nid = numa_mem_id();
  250. align_start = res->start & ~(SECTION_SIZE - 1);
  251. align_size = ALIGN(resource_size(res), SECTION_SIZE);
  252. error = arch_add_memory(nid, align_start, align_size, true);
  253. if (error)
  254. goto err_add_memory;
  255. devres_add(dev, page_map);
  256. return __va(res->start);
  257. err_add_memory:
  258. err_radix:
  259. pgmap_radix_release(res);
  260. devres_free(page_map);
  261. return ERR_PTR(error);
  262. }
  263. EXPORT_SYMBOL(devm_memremap_pages);
  264. unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
  265. {
  266. /* number of pfns from base where pfn_to_page() is valid */
  267. return altmap->reserve + altmap->free;
  268. }
  269. void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
  270. {
  271. altmap->alloc -= nr_pfns;
  272. }
  273. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  274. struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
  275. {
  276. /*
  277. * 'memmap_start' is the virtual address for the first "struct
  278. * page" in this range of the vmemmap array. In the case of
  279. * CONFIG_SPARSE_VMEMMAP a page_to_pfn conversion is simple
  280. * pointer arithmetic, so we can perform this to_vmem_altmap()
  281. * conversion without concern for the initialization state of
  282. * the struct page fields.
  283. */
  284. struct page *page = (struct page *) memmap_start;
  285. struct dev_pagemap *pgmap;
  286. /*
  287. * Uncoditionally retrieve a dev_pagemap associated with the
  288. * given physical address, this is only for use in the
  289. * arch_{add|remove}_memory() for setting up and tearing down
  290. * the memmap.
  291. */
  292. rcu_read_lock();
  293. pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
  294. rcu_read_unlock();
  295. return pgmap ? pgmap->altmap : NULL;
  296. }
  297. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  298. #endif /* CONFIG_ZONE_DEVICE */