memremap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. unsigned long pfn = PHYS_PFN(offset);
  31. /* In the simple case just return the existing linear address */
  32. if (pfn_valid(pfn) && !PageHighMem(pfn_to_page(pfn)))
  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: any of MEMREMAP_WB, MEMREMAP_WT and MEMREMAP_WC
  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. In the case of multiple flags, the different
  45. * mapping types will be attempted in the order listed below until one of
  46. * them succeeds.
  47. *
  48. * MEMREMAP_WB - matches the default mapping for System RAM on
  49. * the architecture. This is usually a read-allocate write-back cache.
  50. * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
  51. * memremap() will bypass establishing a new mapping and instead return
  52. * a pointer into the direct map.
  53. *
  54. * MEMREMAP_WT - establish a mapping whereby writes either bypass the
  55. * cache or are written through to memory and never exist in a
  56. * cache-dirty state with respect to program visibility. Attempts to
  57. * map System RAM with this mapping type will fail.
  58. *
  59. * MEMREMAP_WC - establish a writecombine mapping, whereby writes may
  60. * be coalesced together (e.g. in the CPU's write buffers), but is otherwise
  61. * uncached. Attempts to map System RAM with this mapping type will fail.
  62. */
  63. void *memremap(resource_size_t offset, size_t size, unsigned long flags)
  64. {
  65. int is_ram = region_intersects(offset, size,
  66. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
  67. void *addr = NULL;
  68. if (!flags)
  69. return NULL;
  70. if (is_ram == REGION_MIXED) {
  71. WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
  72. &offset, (unsigned long) size);
  73. return NULL;
  74. }
  75. /* Try all mapping types requested until one returns non-NULL */
  76. if (flags & MEMREMAP_WB) {
  77. /*
  78. * MEMREMAP_WB is special in that it can be satisifed
  79. * from the direct map. Some archs depend on the
  80. * capability of memremap() to autodetect cases where
  81. * the requested range is potentially in System RAM.
  82. */
  83. if (is_ram == REGION_INTERSECTS)
  84. addr = try_ram_remap(offset, size);
  85. if (!addr)
  86. addr = ioremap_cache(offset, size);
  87. }
  88. /*
  89. * If we don't have a mapping yet and other request flags are
  90. * present then we will be attempting to establish a new virtual
  91. * address mapping. Enforce that this mapping is not aliasing
  92. * System RAM.
  93. */
  94. if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
  95. WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
  96. &offset, (unsigned long) size);
  97. return NULL;
  98. }
  99. if (!addr && (flags & MEMREMAP_WT))
  100. addr = ioremap_wt(offset, size);
  101. if (!addr && (flags & MEMREMAP_WC))
  102. addr = ioremap_wc(offset, size);
  103. return addr;
  104. }
  105. EXPORT_SYMBOL(memremap);
  106. void memunmap(void *addr)
  107. {
  108. if (is_vmalloc_addr(addr))
  109. iounmap((void __iomem *) addr);
  110. }
  111. EXPORT_SYMBOL(memunmap);
  112. static void devm_memremap_release(struct device *dev, void *res)
  113. {
  114. memunmap(*(void **)res);
  115. }
  116. static int devm_memremap_match(struct device *dev, void *res, void *match_data)
  117. {
  118. return *(void **)res == match_data;
  119. }
  120. void *devm_memremap(struct device *dev, resource_size_t offset,
  121. size_t size, unsigned long flags)
  122. {
  123. void **ptr, *addr;
  124. ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
  125. dev_to_node(dev));
  126. if (!ptr)
  127. return ERR_PTR(-ENOMEM);
  128. addr = memremap(offset, size, flags);
  129. if (addr) {
  130. *ptr = addr;
  131. devres_add(dev, ptr);
  132. } else {
  133. devres_free(ptr);
  134. return ERR_PTR(-ENXIO);
  135. }
  136. return addr;
  137. }
  138. EXPORT_SYMBOL(devm_memremap);
  139. void devm_memunmap(struct device *dev, void *addr)
  140. {
  141. WARN_ON(devres_release(dev, devm_memremap_release,
  142. devm_memremap_match, addr));
  143. }
  144. EXPORT_SYMBOL(devm_memunmap);
  145. pfn_t phys_to_pfn_t(phys_addr_t addr, u64 flags)
  146. {
  147. return __pfn_to_pfn_t(addr >> PAGE_SHIFT, flags);
  148. }
  149. EXPORT_SYMBOL(phys_to_pfn_t);
  150. #ifdef CONFIG_ZONE_DEVICE
  151. static DEFINE_MUTEX(pgmap_lock);
  152. static RADIX_TREE(pgmap_radix, GFP_KERNEL);
  153. #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
  154. #define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
  155. struct page_map {
  156. struct resource res;
  157. struct percpu_ref *ref;
  158. struct dev_pagemap pgmap;
  159. struct vmem_altmap altmap;
  160. };
  161. void get_zone_device_page(struct page *page)
  162. {
  163. percpu_ref_get(page->pgmap->ref);
  164. }
  165. EXPORT_SYMBOL(get_zone_device_page);
  166. void put_zone_device_page(struct page *page)
  167. {
  168. put_dev_pagemap(page->pgmap);
  169. }
  170. EXPORT_SYMBOL(put_zone_device_page);
  171. static void pgmap_radix_release(struct resource *res)
  172. {
  173. resource_size_t key, align_start, align_size, align_end;
  174. align_start = res->start & ~(SECTION_SIZE - 1);
  175. align_size = ALIGN(resource_size(res), SECTION_SIZE);
  176. align_end = align_start + align_size - 1;
  177. mutex_lock(&pgmap_lock);
  178. for (key = res->start; key <= res->end; key += SECTION_SIZE)
  179. radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT);
  180. mutex_unlock(&pgmap_lock);
  181. }
  182. static unsigned long pfn_first(struct page_map *page_map)
  183. {
  184. struct dev_pagemap *pgmap = &page_map->pgmap;
  185. const struct resource *res = &page_map->res;
  186. struct vmem_altmap *altmap = pgmap->altmap;
  187. unsigned long pfn;
  188. pfn = res->start >> PAGE_SHIFT;
  189. if (altmap)
  190. pfn += vmem_altmap_offset(altmap);
  191. return pfn;
  192. }
  193. static unsigned long pfn_end(struct page_map *page_map)
  194. {
  195. const struct resource *res = &page_map->res;
  196. return (res->start + resource_size(res)) >> PAGE_SHIFT;
  197. }
  198. #define for_each_device_pfn(pfn, map) \
  199. for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++)
  200. static void devm_memremap_pages_release(struct device *dev, void *data)
  201. {
  202. struct page_map *page_map = data;
  203. struct resource *res = &page_map->res;
  204. resource_size_t align_start, align_size;
  205. struct dev_pagemap *pgmap = &page_map->pgmap;
  206. if (percpu_ref_tryget_live(pgmap->ref)) {
  207. dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
  208. percpu_ref_put(pgmap->ref);
  209. }
  210. /* pages are dead and unused, undo the arch mapping */
  211. align_start = res->start & ~(SECTION_SIZE - 1);
  212. align_size = ALIGN(resource_size(res), SECTION_SIZE);
  213. arch_remove_memory(align_start, align_size);
  214. pgmap_radix_release(res);
  215. dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
  216. "%s: failed to free all reserved pages\n", __func__);
  217. }
  218. /* assumes rcu_read_lock() held at entry */
  219. struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
  220. {
  221. struct page_map *page_map;
  222. WARN_ON_ONCE(!rcu_read_lock_held());
  223. page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
  224. return page_map ? &page_map->pgmap : NULL;
  225. }
  226. /**
  227. * devm_memremap_pages - remap and provide memmap backing for the given resource
  228. * @dev: hosting device for @res
  229. * @res: "host memory" address range
  230. * @ref: a live per-cpu reference count
  231. * @altmap: optional descriptor for allocating the memmap from @res
  232. *
  233. * Notes:
  234. * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
  235. * (or devm release event).
  236. *
  237. * 2/ @res is expected to be a host memory range that could feasibly be
  238. * treated as a "System RAM" range, i.e. not a device mmio range, but
  239. * this is not enforced.
  240. */
  241. void *devm_memremap_pages(struct device *dev, struct resource *res,
  242. struct percpu_ref *ref, struct vmem_altmap *altmap)
  243. {
  244. resource_size_t key, align_start, align_size, align_end;
  245. struct dev_pagemap *pgmap;
  246. struct page_map *page_map;
  247. int error, nid, is_ram;
  248. unsigned long pfn;
  249. align_start = res->start & ~(SECTION_SIZE - 1);
  250. align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
  251. - align_start;
  252. is_ram = region_intersects(align_start, align_size,
  253. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
  254. if (is_ram == REGION_MIXED) {
  255. WARN_ONCE(1, "%s attempted on mixed region %pr\n",
  256. __func__, res);
  257. return ERR_PTR(-ENXIO);
  258. }
  259. if (is_ram == REGION_INTERSECTS)
  260. return __va(res->start);
  261. if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) {
  262. dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n",
  263. __func__);
  264. return ERR_PTR(-ENXIO);
  265. }
  266. if (!ref)
  267. return ERR_PTR(-EINVAL);
  268. page_map = devres_alloc_node(devm_memremap_pages_release,
  269. sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
  270. if (!page_map)
  271. return ERR_PTR(-ENOMEM);
  272. pgmap = &page_map->pgmap;
  273. memcpy(&page_map->res, res, sizeof(*res));
  274. pgmap->dev = dev;
  275. if (altmap) {
  276. memcpy(&page_map->altmap, altmap, sizeof(*altmap));
  277. pgmap->altmap = &page_map->altmap;
  278. }
  279. pgmap->ref = ref;
  280. pgmap->res = &page_map->res;
  281. mutex_lock(&pgmap_lock);
  282. error = 0;
  283. align_end = align_start + align_size - 1;
  284. for (key = align_start; key <= align_end; key += SECTION_SIZE) {
  285. struct dev_pagemap *dup;
  286. rcu_read_lock();
  287. dup = find_dev_pagemap(key);
  288. rcu_read_unlock();
  289. if (dup) {
  290. dev_err(dev, "%s: %pr collides with mapping for %s\n",
  291. __func__, res, dev_name(dup->dev));
  292. error = -EBUSY;
  293. break;
  294. }
  295. error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT,
  296. page_map);
  297. if (error) {
  298. dev_err(dev, "%s: failed: %d\n", __func__, error);
  299. break;
  300. }
  301. }
  302. mutex_unlock(&pgmap_lock);
  303. if (error)
  304. goto err_radix;
  305. nid = dev_to_node(dev);
  306. if (nid < 0)
  307. nid = numa_mem_id();
  308. error = arch_add_memory(nid, align_start, align_size, true);
  309. if (error)
  310. goto err_add_memory;
  311. for_each_device_pfn(pfn, page_map) {
  312. struct page *page = pfn_to_page(pfn);
  313. /*
  314. * ZONE_DEVICE pages union ->lru with a ->pgmap back
  315. * pointer. It is a bug if a ZONE_DEVICE page is ever
  316. * freed or placed on a driver-private list. Seed the
  317. * storage with LIST_POISON* values.
  318. */
  319. list_del(&page->lru);
  320. page->pgmap = pgmap;
  321. }
  322. devres_add(dev, page_map);
  323. return __va(res->start);
  324. err_add_memory:
  325. err_radix:
  326. pgmap_radix_release(res);
  327. devres_free(page_map);
  328. return ERR_PTR(error);
  329. }
  330. EXPORT_SYMBOL(devm_memremap_pages);
  331. unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
  332. {
  333. /* number of pfns from base where pfn_to_page() is valid */
  334. return altmap->reserve + altmap->free;
  335. }
  336. void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
  337. {
  338. altmap->alloc -= nr_pfns;
  339. }
  340. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  341. struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
  342. {
  343. /*
  344. * 'memmap_start' is the virtual address for the first "struct
  345. * page" in this range of the vmemmap array. In the case of
  346. * CONFIG_SPARSEMEM_VMEMMAP a page_to_pfn conversion is simple
  347. * pointer arithmetic, so we can perform this to_vmem_altmap()
  348. * conversion without concern for the initialization state of
  349. * the struct page fields.
  350. */
  351. struct page *page = (struct page *) memmap_start;
  352. struct dev_pagemap *pgmap;
  353. /*
  354. * Unconditionally retrieve a dev_pagemap associated with the
  355. * given physical address, this is only for use in the
  356. * arch_{add|remove}_memory() for setting up and tearing down
  357. * the memmap.
  358. */
  359. rcu_read_lock();
  360. pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
  361. rcu_read_unlock();
  362. return pgmap ? pgmap->altmap : NULL;
  363. }
  364. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  365. #endif /* CONFIG_ZONE_DEVICE */