powerpc/eeh: Don't remove passed VFs
[linux/fpc-iii.git] / kernel / memremap.c
blob70ee3775de24ebcf2e80ab2fdad21eaad24dd2fe
1 /*
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>
18 #include <linux/io.h>
19 #include <linux/mm.h>
20 #include <linux/memory_hotplug.h>
22 #ifndef ioremap_cache
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);
28 #endif
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))
36 return __va(offset);
37 return NULL; /* fallback to ioremap_cache */
40 /**
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, "System RAM");
64 void *addr = NULL;
66 if (is_ram == REGION_MIXED) {
67 WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
68 &offset, (unsigned long) size);
69 return NULL;
72 /* Try all mapping types requested until one returns non-NULL */
73 if (flags & MEMREMAP_WB) {
74 flags &= ~MEMREMAP_WB;
76 * MEMREMAP_WB is special in that it can be satisifed
77 * from the direct map. Some archs depend on the
78 * capability of memremap() to autodetect cases where
79 * the requested range is potentially in "System RAM"
81 if (is_ram == REGION_INTERSECTS)
82 addr = try_ram_remap(offset, size);
83 if (!addr)
84 addr = ioremap_cache(offset, size);
88 * If we don't have a mapping yet and more request flags are
89 * pending then we will be attempting to establish a new virtual
90 * address mapping. Enforce that this mapping is not aliasing
91 * "System RAM"
93 if (!addr && is_ram == REGION_INTERSECTS && flags) {
94 WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
95 &offset, (unsigned long) size);
96 return NULL;
99 if (!addr && (flags & MEMREMAP_WT)) {
100 flags &= ~MEMREMAP_WT;
101 addr = ioremap_wt(offset, size);
104 return addr;
106 EXPORT_SYMBOL(memremap);
108 void memunmap(void *addr)
110 if (is_vmalloc_addr(addr))
111 iounmap((void __iomem *) addr);
113 EXPORT_SYMBOL(memunmap);
115 static void devm_memremap_release(struct device *dev, void *res)
117 memunmap(res);
120 static int devm_memremap_match(struct device *dev, void *res, void *match_data)
122 return *(void **)res == match_data;
125 void *devm_memremap(struct device *dev, resource_size_t offset,
126 size_t size, unsigned long flags)
128 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);
135 addr = memremap(offset, size, flags);
136 if (addr) {
137 *ptr = addr;
138 devres_add(dev, ptr);
139 } else
140 devres_free(ptr);
142 return addr;
144 EXPORT_SYMBOL(devm_memremap);
146 void devm_memunmap(struct device *dev, void *addr)
148 WARN_ON(devres_release(dev, devm_memremap_release,
149 devm_memremap_match, addr));
151 EXPORT_SYMBOL(devm_memunmap);
153 pfn_t phys_to_pfn_t(phys_addr_t addr, unsigned long flags)
155 return __pfn_to_pfn_t(addr >> PAGE_SHIFT, flags);
157 EXPORT_SYMBOL(phys_to_pfn_t);
159 #ifdef CONFIG_ZONE_DEVICE
160 static DEFINE_MUTEX(pgmap_lock);
161 static RADIX_TREE(pgmap_radix, GFP_KERNEL);
162 #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
163 #define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
165 struct page_map {
166 struct resource res;
167 struct percpu_ref *ref;
168 struct dev_pagemap pgmap;
169 struct vmem_altmap altmap;
172 void get_zone_device_page(struct page *page)
174 percpu_ref_get(page->pgmap->ref);
176 EXPORT_SYMBOL(get_zone_device_page);
178 void put_zone_device_page(struct page *page)
180 put_dev_pagemap(page->pgmap);
182 EXPORT_SYMBOL(put_zone_device_page);
184 static void pgmap_radix_release(struct resource *res)
186 resource_size_t key, align_start, align_size, align_end;
188 align_start = res->start & ~(SECTION_SIZE - 1);
189 align_size = ALIGN(resource_size(res), SECTION_SIZE);
190 align_end = align_start + align_size - 1;
192 mutex_lock(&pgmap_lock);
193 for (key = res->start; key <= res->end; key += SECTION_SIZE)
194 radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT);
195 mutex_unlock(&pgmap_lock);
198 static unsigned long pfn_first(struct page_map *page_map)
200 struct dev_pagemap *pgmap = &page_map->pgmap;
201 const struct resource *res = &page_map->res;
202 struct vmem_altmap *altmap = pgmap->altmap;
203 unsigned long pfn;
205 pfn = res->start >> PAGE_SHIFT;
206 if (altmap)
207 pfn += vmem_altmap_offset(altmap);
208 return pfn;
211 static unsigned long pfn_end(struct page_map *page_map)
213 const struct resource *res = &page_map->res;
215 return (res->start + resource_size(res)) >> PAGE_SHIFT;
218 #define for_each_device_pfn(pfn, map) \
219 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++)
221 static void devm_memremap_pages_release(struct device *dev, void *data)
223 struct page_map *page_map = data;
224 struct resource *res = &page_map->res;
225 resource_size_t align_start, align_size;
226 struct dev_pagemap *pgmap = &page_map->pgmap;
228 if (percpu_ref_tryget_live(pgmap->ref)) {
229 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
230 percpu_ref_put(pgmap->ref);
233 /* pages are dead and unused, undo the arch mapping */
234 align_start = res->start & ~(SECTION_SIZE - 1);
235 align_size = ALIGN(resource_size(res), SECTION_SIZE);
236 arch_remove_memory(align_start, align_size);
237 pgmap_radix_release(res);
238 dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
239 "%s: failed to free all reserved pages\n", __func__);
242 /* assumes rcu_read_lock() held at entry */
243 struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
245 struct page_map *page_map;
247 WARN_ON_ONCE(!rcu_read_lock_held());
249 page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
250 return page_map ? &page_map->pgmap : NULL;
254 * devm_memremap_pages - remap and provide memmap backing for the given resource
255 * @dev: hosting device for @res
256 * @res: "host memory" address range
257 * @ref: a live per-cpu reference count
258 * @altmap: optional descriptor for allocating the memmap from @res
260 * Notes:
261 * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
262 * (or devm release event).
264 * 2/ @res is expected to be a host memory range that could feasibly be
265 * treated as a "System RAM" range, i.e. not a device mmio range, but
266 * this is not enforced.
268 void *devm_memremap_pages(struct device *dev, struct resource *res,
269 struct percpu_ref *ref, struct vmem_altmap *altmap)
271 int is_ram = region_intersects(res->start, resource_size(res),
272 "System RAM");
273 resource_size_t key, align_start, align_size, align_end;
274 struct dev_pagemap *pgmap;
275 struct page_map *page_map;
276 unsigned long pfn;
277 int error, nid;
279 if (is_ram == REGION_MIXED) {
280 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
281 __func__, res);
282 return ERR_PTR(-ENXIO);
285 if (is_ram == REGION_INTERSECTS)
286 return __va(res->start);
288 if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) {
289 dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n",
290 __func__);
291 return ERR_PTR(-ENXIO);
294 if (!ref)
295 return ERR_PTR(-EINVAL);
297 page_map = devres_alloc_node(devm_memremap_pages_release,
298 sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
299 if (!page_map)
300 return ERR_PTR(-ENOMEM);
301 pgmap = &page_map->pgmap;
303 memcpy(&page_map->res, res, sizeof(*res));
305 pgmap->dev = dev;
306 if (altmap) {
307 memcpy(&page_map->altmap, altmap, sizeof(*altmap));
308 pgmap->altmap = &page_map->altmap;
310 pgmap->ref = ref;
311 pgmap->res = &page_map->res;
313 mutex_lock(&pgmap_lock);
314 error = 0;
315 align_start = res->start & ~(SECTION_SIZE - 1);
316 align_size = ALIGN(resource_size(res), SECTION_SIZE);
317 align_end = align_start + align_size - 1;
318 for (key = align_start; key <= align_end; key += SECTION_SIZE) {
319 struct dev_pagemap *dup;
321 rcu_read_lock();
322 dup = find_dev_pagemap(key);
323 rcu_read_unlock();
324 if (dup) {
325 dev_err(dev, "%s: %pr collides with mapping for %s\n",
326 __func__, res, dev_name(dup->dev));
327 error = -EBUSY;
328 break;
330 error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT,
331 page_map);
332 if (error) {
333 dev_err(dev, "%s: failed: %d\n", __func__, error);
334 break;
337 mutex_unlock(&pgmap_lock);
338 if (error)
339 goto err_radix;
341 nid = dev_to_node(dev);
342 if (nid < 0)
343 nid = numa_mem_id();
345 error = arch_add_memory(nid, align_start, align_size, true);
346 if (error)
347 goto err_add_memory;
349 for_each_device_pfn(pfn, page_map) {
350 struct page *page = pfn_to_page(pfn);
352 /* ZONE_DEVICE pages must never appear on a slab lru */
353 list_force_poison(&page->lru);
354 page->pgmap = pgmap;
356 devres_add(dev, page_map);
357 return __va(res->start);
359 err_add_memory:
360 err_radix:
361 pgmap_radix_release(res);
362 devres_free(page_map);
363 return ERR_PTR(error);
365 EXPORT_SYMBOL(devm_memremap_pages);
367 unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
369 /* number of pfns from base where pfn_to_page() is valid */
370 return altmap->reserve + altmap->free;
373 void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
375 altmap->alloc -= nr_pfns;
378 #ifdef CONFIG_SPARSEMEM_VMEMMAP
379 struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
382 * 'memmap_start' is the virtual address for the first "struct
383 * page" in this range of the vmemmap array. In the case of
384 * CONFIG_SPARSE_VMEMMAP a page_to_pfn conversion is simple
385 * pointer arithmetic, so we can perform this to_vmem_altmap()
386 * conversion without concern for the initialization state of
387 * the struct page fields.
389 struct page *page = (struct page *) memmap_start;
390 struct dev_pagemap *pgmap;
393 * Uncoditionally retrieve a dev_pagemap associated with the
394 * given physical address, this is only for use in the
395 * arch_{add|remove}_memory() for setting up and tearing down
396 * the memmap.
398 rcu_read_lock();
399 pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
400 rcu_read_unlock();
402 return pgmap ? pgmap->altmap : NULL;
404 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
405 #endif /* CONFIG_ZONE_DEVICE */