memremap.c 11 KB

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