memremap.c 11 KB

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