2 * Copyright(c) 2015 Intel Corporation. All rights reserved.
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.
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.
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>
20 #include <linux/memory_hotplug.h>
23 /* temporary while we convert existing ioremap_cache users to memremap */
24 __weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size)
26 return ioremap(offset, size);
30 static void *try_ram_remap(resource_size_t offset, size_t size)
32 struct page *page = pfn_to_page(offset >> PAGE_SHIFT);
34 /* In the simple case just return the existing linear address */
35 if (!PageHighMem(page))
37 return NULL; /* fallback to ioremap_cache */
41 * memremap() - remap an iomem_resource as cacheable memory
42 * @offset: iomem resource start address
43 * @size: size of remap
44 * @flags: either MEMREMAP_WB or MEMREMAP_WT
46 * memremap() is "ioremap" for cases where it is known that the resource
47 * being mapped does not have i/o side effects and the __iomem
48 * annotation is not applicable.
50 * MEMREMAP_WB - matches the default mapping for System RAM on
51 * the architecture. This is usually a read-allocate write-back cache.
52 * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
53 * memremap() will bypass establishing a new mapping and instead return
54 * a pointer into the direct map.
56 * MEMREMAP_WT - establish a mapping whereby writes either bypass the
57 * cache or are written through to memory and never exist in a
58 * cache-dirty state with respect to program visibility. Attempts to
59 * map System RAM with this mapping type will fail.
61 void *memremap(resource_size_t offset, size_t size, unsigned long flags)
63 int is_ram = region_intersects(offset, size,
64 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
67 if (is_ram == REGION_MIXED) {
68 WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
69 &offset, (unsigned long) size);
73 /* Try all mapping types requested until one returns non-NULL */
74 if (flags & MEMREMAP_WB) {
75 flags &= ~MEMREMAP_WB;
77 * MEMREMAP_WB is special in that it can be satisifed
78 * from the direct map. Some archs depend on the
79 * capability of memremap() to autodetect cases where
80 * the requested range is potentially in System RAM.
82 if (is_ram == REGION_INTERSECTS)
83 addr = try_ram_remap(offset, size);
85 addr = ioremap_cache(offset, size);
89 * If we don't have a mapping yet and more request flags are
90 * pending then we will be attempting to establish a new virtual
91 * address mapping. Enforce that this mapping is not aliasing
94 if (!addr && is_ram == REGION_INTERSECTS && flags) {
95 WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
96 &offset, (unsigned long) size);
100 if (!addr && (flags & MEMREMAP_WT)) {
101 flags &= ~MEMREMAP_WT;
102 addr = ioremap_wt(offset, size);
107 EXPORT_SYMBOL(memremap);
109 void memunmap(void *addr)
111 if (is_vmalloc_addr(addr))
112 iounmap((void __iomem *) addr);
114 EXPORT_SYMBOL(memunmap);
116 static void devm_memremap_release(struct device *dev, void *res)
121 static int devm_memremap_match(struct device *dev, void *res, void *match_data)
123 return *(void **)res == match_data;
126 void *devm_memremap(struct device *dev, resource_size_t offset,
127 size_t size, unsigned long flags)
131 ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
134 return ERR_PTR(-ENOMEM);
136 addr = memremap(offset, size, flags);
139 devres_add(dev, ptr);
145 EXPORT_SYMBOL(devm_memremap);
147 void devm_memunmap(struct device *dev, void *addr)
149 WARN_ON(devres_release(dev, devm_memremap_release,
150 devm_memremap_match, addr));
152 EXPORT_SYMBOL(devm_memunmap);
154 pfn_t phys_to_pfn_t(phys_addr_t addr, unsigned long flags)
156 return __pfn_to_pfn_t(addr >> PAGE_SHIFT, flags);
158 EXPORT_SYMBOL(phys_to_pfn_t);
160 #ifdef CONFIG_ZONE_DEVICE
161 static DEFINE_MUTEX(pgmap_lock);
162 static RADIX_TREE(pgmap_radix, GFP_KERNEL);
163 #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
164 #define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
168 struct percpu_ref *ref;
169 struct dev_pagemap pgmap;
170 struct vmem_altmap altmap;
173 void get_zone_device_page(struct page *page)
175 percpu_ref_get(page->pgmap->ref);
177 EXPORT_SYMBOL(get_zone_device_page);
179 void put_zone_device_page(struct page *page)
181 put_dev_pagemap(page->pgmap);
183 EXPORT_SYMBOL(put_zone_device_page);
185 static void pgmap_radix_release(struct resource *res)
187 resource_size_t key, align_start, align_size, align_end;
189 align_start = res->start & ~(SECTION_SIZE - 1);
190 align_size = ALIGN(resource_size(res), SECTION_SIZE);
191 align_end = align_start + align_size - 1;
193 mutex_lock(&pgmap_lock);
194 for (key = res->start; key <= res->end; key += SECTION_SIZE)
195 radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT);
196 mutex_unlock(&pgmap_lock);
199 static unsigned long pfn_first(struct page_map *page_map)
201 struct dev_pagemap *pgmap = &page_map->pgmap;
202 const struct resource *res = &page_map->res;
203 struct vmem_altmap *altmap = pgmap->altmap;
206 pfn = res->start >> PAGE_SHIFT;
208 pfn += vmem_altmap_offset(altmap);
212 static unsigned long pfn_end(struct page_map *page_map)
214 const struct resource *res = &page_map->res;
216 return (res->start + resource_size(res)) >> PAGE_SHIFT;
219 #define for_each_device_pfn(pfn, map) \
220 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++)
222 static void devm_memremap_pages_release(struct device *dev, void *data)
224 struct page_map *page_map = data;
225 struct resource *res = &page_map->res;
226 resource_size_t align_start, align_size;
227 struct dev_pagemap *pgmap = &page_map->pgmap;
229 if (percpu_ref_tryget_live(pgmap->ref)) {
230 dev_WARN(dev, true, "%s: page mapping is still live!\n",
232 percpu_ref_put(pgmap->ref);
235 /* pages are dead and unused, undo the arch mapping */
236 align_start = res->start & ~(SECTION_SIZE - 1);
237 align_size = ALIGN(resource_size(res), SECTION_SIZE);
238 arch_remove_memory(align_start, align_size);
239 pgmap_radix_release(res);
240 dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
241 "%s: failed to free all reserved pages\n", __func__);
244 /* assumes rcu_read_lock() held at entry */
245 struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
247 struct page_map *page_map;
249 WARN_ON_ONCE(!rcu_read_lock_held());
251 page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
252 return page_map ? &page_map->pgmap : NULL;
256 * devm_memremap_pages - remap and provide memmap backing for the given resource
257 * @dev: hosting device for @res
258 * @res: "host memory" address range
259 * @ref: a live per-cpu reference count
260 * @altmap: optional descriptor for allocating the memmap from @res
263 * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
264 * (or devm release event).
266 * 2/ @res is expected to be a host memory range that could feasibly be
267 * treated as a "System RAM" range, i.e. not a device mmio range, but
268 * this is not enforced.
270 void *devm_memremap_pages(struct device *dev, struct resource *res,
271 struct percpu_ref *ref, struct vmem_altmap *altmap)
273 int is_ram = region_intersects(res->start, resource_size(res),
274 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
275 resource_size_t key, align_start, align_size, align_end;
276 struct dev_pagemap *pgmap;
277 struct page_map *page_map;
281 if (is_ram == REGION_MIXED) {
282 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
284 return ERR_PTR(-ENXIO);
287 if (is_ram == REGION_INTERSECTS)
288 return __va(res->start);
290 if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) {
291 dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n",
293 return ERR_PTR(-ENXIO);
297 return ERR_PTR(-EINVAL);
299 page_map = devres_alloc_node(devm_memremap_pages_release,
300 sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
302 return ERR_PTR(-ENOMEM);
303 pgmap = &page_map->pgmap;
305 memcpy(&page_map->res, res, sizeof(*res));
309 memcpy(&page_map->altmap, altmap, sizeof(*altmap));
310 pgmap->altmap = &page_map->altmap;
313 pgmap->res = &page_map->res;
315 mutex_lock(&pgmap_lock);
317 align_start = res->start & ~(SECTION_SIZE - 1);
318 align_size = ALIGN(resource_size(res), SECTION_SIZE);
319 align_end = align_start + align_size - 1;
320 for (key = align_start; key <= align_end; key += SECTION_SIZE) {
321 struct dev_pagemap *dup;
324 dup = find_dev_pagemap(key);
327 dev_err(dev, "%s: %pr collides with mapping for %s\n",
328 __func__, res, dev_name(dup->dev));
332 error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT,
335 dev_err(dev, "%s: failed: %d\n", __func__, error);
339 mutex_unlock(&pgmap_lock);
343 nid = dev_to_node(dev);
347 error = arch_add_memory(nid, align_start, align_size, true);
351 for_each_device_pfn(pfn, page_map) {
352 struct page *page = pfn_to_page(pfn);
354 /* ZONE_DEVICE pages must never appear on a slab lru */
355 list_force_poison(&page->lru);
358 devres_add(dev, page_map);
359 return __va(res->start);
363 pgmap_radix_release(res);
364 devres_free(page_map);
365 return ERR_PTR(error);
367 EXPORT_SYMBOL(devm_memremap_pages);
369 unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
371 /* number of pfns from base where pfn_to_page() is valid */
372 return altmap->reserve + altmap->free;
375 void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
377 altmap->alloc -= nr_pfns;
380 #ifdef CONFIG_SPARSEMEM_VMEMMAP
381 struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
384 * 'memmap_start' is the virtual address for the first "struct
385 * page" in this range of the vmemmap array. In the case of
386 * CONFIG_SPARSE_VMEMMAP a page_to_pfn conversion is simple
387 * pointer arithmetic, so we can perform this to_vmem_altmap()
388 * conversion without concern for the initialization state of
389 * the struct page fields.
391 struct page *page = (struct page *) memmap_start;
392 struct dev_pagemap *pgmap;
395 * Uncoditionally retrieve a dev_pagemap associated with the
396 * given physical address, this is only for use in the
397 * arch_{add|remove}_memory() for setting up and tearing down
401 pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
404 return pgmap ? pgmap->altmap : NULL;
406 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
407 #endif /* CONFIG_ZONE_DEVICE */