memremap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. void get_zone_device_page(struct page *page)
  152. {
  153. percpu_ref_get(page->pgmap->ref);
  154. }
  155. EXPORT_SYMBOL(get_zone_device_page);
  156. void put_zone_device_page(struct page *page)
  157. {
  158. put_dev_pagemap(page->pgmap);
  159. }
  160. EXPORT_SYMBOL(put_zone_device_page);
  161. static void pgmap_radix_release(struct resource *res)
  162. {
  163. resource_size_t key;
  164. mutex_lock(&pgmap_lock);
  165. for (key = res->start; key <= res->end; key += SECTION_SIZE)
  166. radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT);
  167. mutex_unlock(&pgmap_lock);
  168. }
  169. static unsigned long pfn_first(struct page_map *page_map)
  170. {
  171. struct dev_pagemap *pgmap = &page_map->pgmap;
  172. const struct resource *res = &page_map->res;
  173. struct vmem_altmap *altmap = pgmap->altmap;
  174. unsigned long pfn;
  175. pfn = res->start >> PAGE_SHIFT;
  176. if (altmap)
  177. pfn += vmem_altmap_offset(altmap);
  178. return pfn;
  179. }
  180. static unsigned long pfn_end(struct page_map *page_map)
  181. {
  182. const struct resource *res = &page_map->res;
  183. return (res->start + resource_size(res)) >> PAGE_SHIFT;
  184. }
  185. #define for_each_device_pfn(pfn, map) \
  186. for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++)
  187. static void devm_memremap_pages_release(struct device *dev, void *data)
  188. {
  189. struct page_map *page_map = data;
  190. struct resource *res = &page_map->res;
  191. resource_size_t align_start, align_size;
  192. struct dev_pagemap *pgmap = &page_map->pgmap;
  193. if (percpu_ref_tryget_live(pgmap->ref)) {
  194. dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
  195. percpu_ref_put(pgmap->ref);
  196. }
  197. pgmap_radix_release(res);
  198. /* pages are dead and unused, undo the arch mapping */
  199. align_start = res->start & ~(SECTION_SIZE - 1);
  200. align_size = ALIGN(resource_size(res), SECTION_SIZE);
  201. arch_remove_memory(align_start, align_size);
  202. dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
  203. "%s: failed to free all reserved pages\n", __func__);
  204. }
  205. /* assumes rcu_read_lock() held at entry */
  206. struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
  207. {
  208. struct page_map *page_map;
  209. WARN_ON_ONCE(!rcu_read_lock_held());
  210. page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
  211. return page_map ? &page_map->pgmap : NULL;
  212. }
  213. /**
  214. * devm_memremap_pages - remap and provide memmap backing for the given resource
  215. * @dev: hosting device for @res
  216. * @res: "host memory" address range
  217. * @ref: a live per-cpu reference count
  218. * @altmap: optional descriptor for allocating the memmap from @res
  219. *
  220. * Notes:
  221. * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
  222. * (or devm release event).
  223. *
  224. * 2/ @res is expected to be a host memory range that could feasibly be
  225. * treated as a "System RAM" range, i.e. not a device mmio range, but
  226. * this is not enforced.
  227. */
  228. void *devm_memremap_pages(struct device *dev, struct resource *res,
  229. struct percpu_ref *ref, struct vmem_altmap *altmap)
  230. {
  231. int is_ram = region_intersects(res->start, resource_size(res),
  232. "System RAM");
  233. resource_size_t key, align_start, align_size;
  234. struct dev_pagemap *pgmap;
  235. struct page_map *page_map;
  236. unsigned long pfn;
  237. int error, nid;
  238. if (is_ram == REGION_MIXED) {
  239. WARN_ONCE(1, "%s attempted on mixed region %pr\n",
  240. __func__, res);
  241. return ERR_PTR(-ENXIO);
  242. }
  243. if (is_ram == REGION_INTERSECTS)
  244. return __va(res->start);
  245. if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) {
  246. dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n",
  247. __func__);
  248. return ERR_PTR(-ENXIO);
  249. }
  250. if (!ref)
  251. return ERR_PTR(-EINVAL);
  252. page_map = devres_alloc_node(devm_memremap_pages_release,
  253. sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
  254. if (!page_map)
  255. return ERR_PTR(-ENOMEM);
  256. pgmap = &page_map->pgmap;
  257. memcpy(&page_map->res, res, sizeof(*res));
  258. pgmap->dev = dev;
  259. if (altmap) {
  260. memcpy(&page_map->altmap, altmap, sizeof(*altmap));
  261. pgmap->altmap = &page_map->altmap;
  262. }
  263. pgmap->ref = ref;
  264. pgmap->res = &page_map->res;
  265. mutex_lock(&pgmap_lock);
  266. error = 0;
  267. for (key = res->start; key <= res->end; key += SECTION_SIZE) {
  268. struct dev_pagemap *dup;
  269. rcu_read_lock();
  270. dup = find_dev_pagemap(key);
  271. rcu_read_unlock();
  272. if (dup) {
  273. dev_err(dev, "%s: %pr collides with mapping for %s\n",
  274. __func__, res, dev_name(dup->dev));
  275. error = -EBUSY;
  276. break;
  277. }
  278. error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT,
  279. page_map);
  280. if (error) {
  281. dev_err(dev, "%s: failed: %d\n", __func__, error);
  282. break;
  283. }
  284. }
  285. mutex_unlock(&pgmap_lock);
  286. if (error)
  287. goto err_radix;
  288. nid = dev_to_node(dev);
  289. if (nid < 0)
  290. nid = numa_mem_id();
  291. align_start = res->start & ~(SECTION_SIZE - 1);
  292. align_size = ALIGN(resource_size(res), SECTION_SIZE);
  293. error = arch_add_memory(nid, align_start, align_size, true);
  294. if (error)
  295. goto err_add_memory;
  296. for_each_device_pfn(pfn, page_map) {
  297. struct page *page = pfn_to_page(pfn);
  298. /* ZONE_DEVICE pages must never appear on a slab lru */
  299. list_force_poison(&page->lru);
  300. page->pgmap = pgmap;
  301. }
  302. devres_add(dev, page_map);
  303. return __va(res->start);
  304. err_add_memory:
  305. err_radix:
  306. pgmap_radix_release(res);
  307. devres_free(page_map);
  308. return ERR_PTR(error);
  309. }
  310. EXPORT_SYMBOL(devm_memremap_pages);
  311. unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
  312. {
  313. /* number of pfns from base where pfn_to_page() is valid */
  314. return altmap->reserve + altmap->free;
  315. }
  316. void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
  317. {
  318. altmap->alloc -= nr_pfns;
  319. }
  320. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  321. struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
  322. {
  323. /*
  324. * 'memmap_start' is the virtual address for the first "struct
  325. * page" in this range of the vmemmap array. In the case of
  326. * CONFIG_SPARSE_VMEMMAP a page_to_pfn conversion is simple
  327. * pointer arithmetic, so we can perform this to_vmem_altmap()
  328. * conversion without concern for the initialization state of
  329. * the struct page fields.
  330. */
  331. struct page *page = (struct page *) memmap_start;
  332. struct dev_pagemap *pgmap;
  333. /*
  334. * Uncoditionally retrieve a dev_pagemap associated with the
  335. * given physical address, this is only for use in the
  336. * arch_{add|remove}_memory() for setting up and tearing down
  337. * the memmap.
  338. */
  339. rcu_read_lock();
  340. pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
  341. rcu_read_unlock();
  342. return pgmap ? pgmap->altmap : NULL;
  343. }
  344. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  345. #endif /* CONFIG_ZONE_DEVICE */