memremap.c 12 KB

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