memremap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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, resource_size_t end_key)
  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. if (key >= end_key)
  181. break;
  182. radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT);
  183. }
  184. mutex_unlock(&pgmap_lock);
  185. }
  186. static unsigned long pfn_first(struct page_map *page_map)
  187. {
  188. struct dev_pagemap *pgmap = &page_map->pgmap;
  189. const struct resource *res = &page_map->res;
  190. struct vmem_altmap *altmap = pgmap->altmap;
  191. unsigned long pfn;
  192. pfn = res->start >> PAGE_SHIFT;
  193. if (altmap)
  194. pfn += vmem_altmap_offset(altmap);
  195. return pfn;
  196. }
  197. static unsigned long pfn_end(struct page_map *page_map)
  198. {
  199. const struct resource *res = &page_map->res;
  200. return (res->start + resource_size(res)) >> PAGE_SHIFT;
  201. }
  202. #define for_each_device_pfn(pfn, map) \
  203. for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++)
  204. static void devm_memremap_pages_release(struct device *dev, void *data)
  205. {
  206. struct page_map *page_map = data;
  207. struct resource *res = &page_map->res;
  208. resource_size_t align_start, align_size;
  209. struct dev_pagemap *pgmap = &page_map->pgmap;
  210. if (percpu_ref_tryget_live(pgmap->ref)) {
  211. dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
  212. percpu_ref_put(pgmap->ref);
  213. }
  214. /* pages are dead and unused, undo the arch mapping */
  215. align_start = res->start & ~(SECTION_SIZE - 1);
  216. align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
  217. - align_start;
  218. lock_device_hotplug();
  219. mem_hotplug_begin();
  220. arch_remove_memory(align_start, align_size);
  221. mem_hotplug_done();
  222. unlock_device_hotplug();
  223. untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
  224. pgmap_radix_release(res, -1);
  225. dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
  226. "%s: failed to free all reserved pages\n", __func__);
  227. }
  228. /* assumes rcu_read_lock() held at entry */
  229. struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
  230. {
  231. struct page_map *page_map;
  232. WARN_ON_ONCE(!rcu_read_lock_held());
  233. page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
  234. return page_map ? &page_map->pgmap : NULL;
  235. }
  236. /**
  237. * devm_memremap_pages - remap and provide memmap backing for the given resource
  238. * @dev: hosting device for @res
  239. * @res: "host memory" address range
  240. * @ref: a live per-cpu reference count
  241. * @altmap: optional descriptor for allocating the memmap from @res
  242. *
  243. * Notes:
  244. * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
  245. * (or devm release event).
  246. *
  247. * 2/ @res is expected to be a host memory range that could feasibly be
  248. * treated as a "System RAM" range, i.e. not a device mmio range, but
  249. * this is not enforced.
  250. */
  251. void *devm_memremap_pages(struct device *dev, struct resource *res,
  252. struct percpu_ref *ref, struct vmem_altmap *altmap)
  253. {
  254. resource_size_t key = 0, align_start, align_size, align_end;
  255. pgprot_t pgprot = PAGE_KERNEL;
  256. struct dev_pagemap *pgmap;
  257. struct page_map *page_map;
  258. int error, nid, is_ram;
  259. unsigned long pfn;
  260. align_start = res->start & ~(SECTION_SIZE - 1);
  261. align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
  262. - align_start;
  263. is_ram = region_intersects(align_start, align_size,
  264. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
  265. if (is_ram == REGION_MIXED) {
  266. WARN_ONCE(1, "%s attempted on mixed region %pr\n",
  267. __func__, res);
  268. return ERR_PTR(-ENXIO);
  269. }
  270. if (is_ram == REGION_INTERSECTS)
  271. return __va(res->start);
  272. if (!ref)
  273. return ERR_PTR(-EINVAL);
  274. page_map = devres_alloc_node(devm_memremap_pages_release,
  275. sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
  276. if (!page_map)
  277. return ERR_PTR(-ENOMEM);
  278. pgmap = &page_map->pgmap;
  279. memcpy(&page_map->res, res, sizeof(*res));
  280. pgmap->dev = dev;
  281. if (altmap) {
  282. memcpy(&page_map->altmap, altmap, sizeof(*altmap));
  283. pgmap->altmap = &page_map->altmap;
  284. }
  285. pgmap->ref = ref;
  286. pgmap->res = &page_map->res;
  287. mutex_lock(&pgmap_lock);
  288. error = 0;
  289. align_end = align_start + align_size - 1;
  290. for (key = align_start; key <= align_end; key += SECTION_SIZE) {
  291. struct dev_pagemap *dup;
  292. rcu_read_lock();
  293. dup = find_dev_pagemap(key);
  294. rcu_read_unlock();
  295. if (dup) {
  296. dev_err(dev, "%s: %pr collides with mapping for %s\n",
  297. __func__, res, dev_name(dup->dev));
  298. error = -EBUSY;
  299. break;
  300. }
  301. error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT,
  302. page_map);
  303. if (error) {
  304. dev_err(dev, "%s: failed: %d\n", __func__, error);
  305. break;
  306. }
  307. }
  308. mutex_unlock(&pgmap_lock);
  309. if (error)
  310. goto err_radix;
  311. nid = dev_to_node(dev);
  312. if (nid < 0)
  313. nid = numa_mem_id();
  314. error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0,
  315. align_size);
  316. if (error)
  317. goto err_pfn_remap;
  318. lock_device_hotplug();
  319. mem_hotplug_begin();
  320. error = arch_add_memory(nid, align_start, align_size, true);
  321. mem_hotplug_done();
  322. unlock_device_hotplug();
  323. if (error)
  324. goto err_add_memory;
  325. for_each_device_pfn(pfn, page_map) {
  326. struct page *page = pfn_to_page(pfn);
  327. /*
  328. * ZONE_DEVICE pages union ->lru with a ->pgmap back
  329. * pointer. It is a bug if a ZONE_DEVICE page is ever
  330. * freed or placed on a driver-private list. Seed the
  331. * storage with LIST_POISON* values.
  332. */
  333. list_del(&page->lru);
  334. page->pgmap = pgmap;
  335. }
  336. devres_add(dev, page_map);
  337. return __va(res->start);
  338. err_add_memory:
  339. untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
  340. err_pfn_remap:
  341. err_radix:
  342. pgmap_radix_release(res, key);
  343. devres_free(page_map);
  344. return ERR_PTR(error);
  345. }
  346. EXPORT_SYMBOL(devm_memremap_pages);
  347. unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
  348. {
  349. /* number of pfns from base where pfn_to_page() is valid */
  350. return altmap->reserve + altmap->free;
  351. }
  352. void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
  353. {
  354. altmap->alloc -= nr_pfns;
  355. }
  356. struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
  357. {
  358. /*
  359. * 'memmap_start' is the virtual address for the first "struct
  360. * page" in this range of the vmemmap array. In the case of
  361. * CONFIG_SPARSEMEM_VMEMMAP a page_to_pfn conversion is simple
  362. * pointer arithmetic, so we can perform this to_vmem_altmap()
  363. * conversion without concern for the initialization state of
  364. * the struct page fields.
  365. */
  366. struct page *page = (struct page *) memmap_start;
  367. struct dev_pagemap *pgmap;
  368. /*
  369. * Unconditionally retrieve a dev_pagemap associated with the
  370. * given physical address, this is only for use in the
  371. * arch_{add|remove}_memory() for setting up and tearing down
  372. * the memmap.
  373. */
  374. rcu_read_lock();
  375. pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
  376. rcu_read_unlock();
  377. return pgmap ? pgmap->altmap : NULL;
  378. }
  379. #endif /* CONFIG_ZONE_DEVICE */