memremap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. mem_hotplug_begin();
  215. arch_remove_memory(align_start, align_size);
  216. mem_hotplug_done();
  217. untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
  218. pgmap_radix_release(res);
  219. dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
  220. "%s: failed to free all reserved pages\n", __func__);
  221. }
  222. /* assumes rcu_read_lock() held at entry */
  223. struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
  224. {
  225. struct page_map *page_map;
  226. WARN_ON_ONCE(!rcu_read_lock_held());
  227. page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
  228. return page_map ? &page_map->pgmap : NULL;
  229. }
  230. /**
  231. * devm_memremap_pages - remap and provide memmap backing for the given resource
  232. * @dev: hosting device for @res
  233. * @res: "host memory" address range
  234. * @ref: a live per-cpu reference count
  235. * @altmap: optional descriptor for allocating the memmap from @res
  236. *
  237. * Notes:
  238. * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
  239. * (or devm release event).
  240. *
  241. * 2/ @res is expected to be a host memory range that could feasibly be
  242. * treated as a "System RAM" range, i.e. not a device mmio range, but
  243. * this is not enforced.
  244. */
  245. void *devm_memremap_pages(struct device *dev, struct resource *res,
  246. struct percpu_ref *ref, struct vmem_altmap *altmap)
  247. {
  248. resource_size_t key, align_start, align_size, align_end;
  249. pgprot_t pgprot = PAGE_KERNEL;
  250. struct dev_pagemap *pgmap;
  251. struct page_map *page_map;
  252. int error, nid, is_ram;
  253. unsigned long pfn;
  254. align_start = res->start & ~(SECTION_SIZE - 1);
  255. align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
  256. - align_start;
  257. is_ram = region_intersects(align_start, align_size,
  258. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
  259. if (is_ram == REGION_MIXED) {
  260. WARN_ONCE(1, "%s attempted on mixed region %pr\n",
  261. __func__, res);
  262. return ERR_PTR(-ENXIO);
  263. }
  264. if (is_ram == REGION_INTERSECTS)
  265. return __va(res->start);
  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 = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0,
  309. align_size);
  310. if (error)
  311. goto err_pfn_remap;
  312. mem_hotplug_begin();
  313. error = arch_add_memory(nid, align_start, align_size, true);
  314. mem_hotplug_done();
  315. if (error)
  316. goto err_add_memory;
  317. for_each_device_pfn(pfn, page_map) {
  318. struct page *page = pfn_to_page(pfn);
  319. /*
  320. * ZONE_DEVICE pages union ->lru with a ->pgmap back
  321. * pointer. It is a bug if a ZONE_DEVICE page is ever
  322. * freed or placed on a driver-private list. Seed the
  323. * storage with LIST_POISON* values.
  324. */
  325. list_del(&page->lru);
  326. page->pgmap = pgmap;
  327. }
  328. devres_add(dev, page_map);
  329. return __va(res->start);
  330. err_add_memory:
  331. untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
  332. err_pfn_remap:
  333. err_radix:
  334. pgmap_radix_release(res);
  335. devres_free(page_map);
  336. return ERR_PTR(error);
  337. }
  338. EXPORT_SYMBOL(devm_memremap_pages);
  339. unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
  340. {
  341. /* number of pfns from base where pfn_to_page() is valid */
  342. return altmap->reserve + altmap->free;
  343. }
  344. void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
  345. {
  346. altmap->alloc -= nr_pfns;
  347. }
  348. struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
  349. {
  350. /*
  351. * 'memmap_start' is the virtual address for the first "struct
  352. * page" in this range of the vmemmap array. In the case of
  353. * CONFIG_SPARSEMEM_VMEMMAP a page_to_pfn conversion is simple
  354. * pointer arithmetic, so we can perform this to_vmem_altmap()
  355. * conversion without concern for the initialization state of
  356. * the struct page fields.
  357. */
  358. struct page *page = (struct page *) memmap_start;
  359. struct dev_pagemap *pgmap;
  360. /*
  361. * Unconditionally retrieve a dev_pagemap associated with the
  362. * given physical address, this is only for use in the
  363. * arch_{add|remove}_memory() for setting up and tearing down
  364. * the memmap.
  365. */
  366. rcu_read_lock();
  367. pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
  368. rcu_read_unlock();
  369. return pgmap ? pgmap->altmap : NULL;
  370. }
  371. #endif /* CONFIG_ZONE_DEVICE */