mm, dax: dax-pmd vs thp-pmd vs hugetlbfs-pmd
[linux-2.6-block.git] / kernel / memremap.c
CommitLineData
92281dee
DW
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 */
9476df7d
DW
13#include <linux/radix-tree.h>
14#include <linux/memremap.h>
7d3dcf26 15#include <linux/device.h>
92281dee 16#include <linux/types.h>
34c0fd54 17#include <linux/pfn_t.h>
92281dee
DW
18#include <linux/io.h>
19#include <linux/mm.h>
41e94a85 20#include <linux/memory_hotplug.h>
92281dee
DW
21
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)
25{
26 return ioremap(offset, size);
27}
28#endif
29
182475b7
DW
30static void *try_ram_remap(resource_size_t offset, size_t size)
31{
32 struct page *page = pfn_to_page(offset >> PAGE_SHIFT);
33
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 */
38}
39
92281dee
DW
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
45 *
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.
49 *
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.
55 *
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.
60 */
61void *memremap(resource_size_t offset, size_t size, unsigned long flags)
62{
63 int is_ram = region_intersects(offset, size, "System RAM");
64 void *addr = NULL;
65
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;
70 }
71
72 /* Try all mapping types requested until one returns non-NULL */
73 if (flags & MEMREMAP_WB) {
74 flags &= ~MEMREMAP_WB;
75 /*
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"
80 */
81 if (is_ram == REGION_INTERSECTS)
182475b7
DW
82 addr = try_ram_remap(offset, size);
83 if (!addr)
92281dee
DW
84 addr = ioremap_cache(offset, size);
85 }
86
87 /*
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"
92 */
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;
97 }
98
99 if (!addr && (flags & MEMREMAP_WT)) {
100 flags &= ~MEMREMAP_WT;
101 addr = ioremap_wt(offset, size);
102 }
103
104 return addr;
105}
106EXPORT_SYMBOL(memremap);
107
108void memunmap(void *addr)
109{
110 if (is_vmalloc_addr(addr))
111 iounmap((void __iomem *) addr);
112}
113EXPORT_SYMBOL(memunmap);
7d3dcf26
CH
114
115static void devm_memremap_release(struct device *dev, void *res)
116{
117 memunmap(res);
118}
119
120static int devm_memremap_match(struct device *dev, void *res, void *match_data)
121{
122 return *(void **)res == match_data;
123}
124
125void *devm_memremap(struct device *dev, resource_size_t offset,
126 size_t size, unsigned long flags)
127{
128 void **ptr, *addr;
129
538ea4aa
DW
130 ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
131 dev_to_node(dev));
7d3dcf26 132 if (!ptr)
b36f4761 133 return ERR_PTR(-ENOMEM);
7d3dcf26
CH
134
135 addr = memremap(offset, size, flags);
136 if (addr) {
137 *ptr = addr;
138 devres_add(dev, ptr);
139 } else
140 devres_free(ptr);
141
142 return addr;
143}
144EXPORT_SYMBOL(devm_memremap);
145
146void devm_memunmap(struct device *dev, void *addr)
147{
d741314f
DW
148 WARN_ON(devres_release(dev, devm_memremap_release,
149 devm_memremap_match, addr));
7d3dcf26
CH
150}
151EXPORT_SYMBOL(devm_memunmap);
41e94a85 152
34c0fd54
DW
153pfn_t phys_to_pfn_t(dma_addr_t addr, unsigned long flags)
154{
155 return __pfn_to_pfn_t(addr >> PAGE_SHIFT, flags);
156}
157EXPORT_SYMBOL(phys_to_pfn_t);
158
41e94a85 159#ifdef CONFIG_ZONE_DEVICE
9476df7d
DW
160static DEFINE_MUTEX(pgmap_lock);
161static RADIX_TREE(pgmap_radix, GFP_KERNEL);
162#define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
163#define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
164
41e94a85
CH
165struct page_map {
166 struct resource res;
9476df7d
DW
167 struct percpu_ref *ref;
168 struct dev_pagemap pgmap;
4b94ffdc 169 struct vmem_altmap altmap;
41e94a85
CH
170};
171
9476df7d
DW
172static void pgmap_radix_release(struct resource *res)
173{
174 resource_size_t key;
175
176 mutex_lock(&pgmap_lock);
177 for (key = res->start; key <= res->end; key += SECTION_SIZE)
178 radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT);
179 mutex_unlock(&pgmap_lock);
180}
181
5c2c2587
DW
182static unsigned long pfn_first(struct page_map *page_map)
183{
184 struct dev_pagemap *pgmap = &page_map->pgmap;
185 const struct resource *res = &page_map->res;
186 struct vmem_altmap *altmap = pgmap->altmap;
187 unsigned long pfn;
188
189 pfn = res->start >> PAGE_SHIFT;
190 if (altmap)
191 pfn += vmem_altmap_offset(altmap);
192 return pfn;
193}
194
195static unsigned long pfn_end(struct page_map *page_map)
196{
197 const struct resource *res = &page_map->res;
198
199 return (res->start + resource_size(res)) >> PAGE_SHIFT;
200}
201
202#define for_each_device_pfn(pfn, map) \
203 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++)
204
9476df7d 205static void devm_memremap_pages_release(struct device *dev, void *data)
41e94a85 206{
9476df7d
DW
207 struct page_map *page_map = data;
208 struct resource *res = &page_map->res;
209 resource_size_t align_start, align_size;
4b94ffdc 210 struct dev_pagemap *pgmap = &page_map->pgmap;
9476df7d 211
5c2c2587
DW
212 if (percpu_ref_tryget_live(pgmap->ref)) {
213 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
214 percpu_ref_put(pgmap->ref);
215 }
216
9476df7d 217 pgmap_radix_release(res);
41e94a85
CH
218
219 /* pages are dead and unused, undo the arch mapping */
9476df7d
DW
220 align_start = res->start & ~(SECTION_SIZE - 1);
221 align_size = ALIGN(resource_size(res), SECTION_SIZE);
222 arch_remove_memory(align_start, align_size);
4b94ffdc
DW
223 dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
224 "%s: failed to free all reserved pages\n", __func__);
9476df7d
DW
225}
226
227/* assumes rcu_read_lock() held at entry */
228struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
229{
230 struct page_map *page_map;
231
232 WARN_ON_ONCE(!rcu_read_lock_held());
233
234 page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
235 return page_map ? &page_map->pgmap : NULL;
41e94a85
CH
236}
237
4b94ffdc
DW
238/**
239 * devm_memremap_pages - remap and provide memmap backing for the given resource
240 * @dev: hosting device for @res
241 * @res: "host memory" address range
5c2c2587 242 * @ref: a live per-cpu reference count
4b94ffdc
DW
243 * @altmap: optional descriptor for allocating the memmap from @res
244 *
5c2c2587
DW
245 * Notes:
246 * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
247 * (or devm release event).
248 *
249 * 2/ @res is expected to be a host memory range that could feasibly be
250 * treated as a "System RAM" range, i.e. not a device mmio range, but
251 * this is not enforced.
4b94ffdc
DW
252 */
253void *devm_memremap_pages(struct device *dev, struct resource *res,
5c2c2587 254 struct percpu_ref *ref, struct vmem_altmap *altmap)
41e94a85
CH
255{
256 int is_ram = region_intersects(res->start, resource_size(res),
257 "System RAM");
9476df7d 258 resource_size_t key, align_start, align_size;
4b94ffdc 259 struct dev_pagemap *pgmap;
41e94a85 260 struct page_map *page_map;
5c2c2587 261 unsigned long pfn;
41e94a85
CH
262 int error, nid;
263
264 if (is_ram == REGION_MIXED) {
265 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
266 __func__, res);
267 return ERR_PTR(-ENXIO);
268 }
269
270 if (is_ram == REGION_INTERSECTS)
271 return __va(res->start);
272
4b94ffdc
DW
273 if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) {
274 dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n",
275 __func__);
276 return ERR_PTR(-ENXIO);
277 }
278
5c2c2587
DW
279 if (!ref)
280 return ERR_PTR(-EINVAL);
281
538ea4aa
DW
282 page_map = devres_alloc_node(devm_memremap_pages_release,
283 sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
41e94a85
CH
284 if (!page_map)
285 return ERR_PTR(-ENOMEM);
4b94ffdc 286 pgmap = &page_map->pgmap;
41e94a85
CH
287
288 memcpy(&page_map->res, res, sizeof(*res));
289
4b94ffdc
DW
290 pgmap->dev = dev;
291 if (altmap) {
292 memcpy(&page_map->altmap, altmap, sizeof(*altmap));
293 pgmap->altmap = &page_map->altmap;
294 }
5c2c2587 295 pgmap->ref = ref;
4b94ffdc
DW
296 pgmap->res = &page_map->res;
297
9476df7d
DW
298 mutex_lock(&pgmap_lock);
299 error = 0;
300 for (key = res->start; key <= res->end; key += SECTION_SIZE) {
301 struct dev_pagemap *dup;
302
303 rcu_read_lock();
304 dup = find_dev_pagemap(key);
305 rcu_read_unlock();
306 if (dup) {
307 dev_err(dev, "%s: %pr collides with mapping for %s\n",
308 __func__, res, dev_name(dup->dev));
309 error = -EBUSY;
310 break;
311 }
312 error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT,
313 page_map);
314 if (error) {
315 dev_err(dev, "%s: failed: %d\n", __func__, error);
316 break;
317 }
318 }
319 mutex_unlock(&pgmap_lock);
320 if (error)
321 goto err_radix;
322
41e94a85
CH
323 nid = dev_to_node(dev);
324 if (nid < 0)
7eff93b7 325 nid = numa_mem_id();
41e94a85 326
9476df7d
DW
327 align_start = res->start & ~(SECTION_SIZE - 1);
328 align_size = ALIGN(resource_size(res), SECTION_SIZE);
329 error = arch_add_memory(nid, align_start, align_size, true);
330 if (error)
331 goto err_add_memory;
41e94a85 332
5c2c2587
DW
333 for_each_device_pfn(pfn, page_map) {
334 struct page *page = pfn_to_page(pfn);
335
336 /* ZONE_DEVICE pages must never appear on a slab lru */
337 list_force_poison(&page->lru);
338 page->pgmap = pgmap;
339 }
41e94a85
CH
340 devres_add(dev, page_map);
341 return __va(res->start);
9476df7d
DW
342
343 err_add_memory:
344 err_radix:
345 pgmap_radix_release(res);
346 devres_free(page_map);
347 return ERR_PTR(error);
41e94a85
CH
348}
349EXPORT_SYMBOL(devm_memremap_pages);
4b94ffdc
DW
350
351unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
352{
353 /* number of pfns from base where pfn_to_page() is valid */
354 return altmap->reserve + altmap->free;
355}
356
357void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
358{
359 altmap->alloc -= nr_pfns;
360}
361
362#ifdef CONFIG_SPARSEMEM_VMEMMAP
363struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
364{
365 /*
366 * 'memmap_start' is the virtual address for the first "struct
367 * page" in this range of the vmemmap array. In the case of
368 * CONFIG_SPARSE_VMEMMAP a page_to_pfn conversion is simple
369 * pointer arithmetic, so we can perform this to_vmem_altmap()
370 * conversion without concern for the initialization state of
371 * the struct page fields.
372 */
373 struct page *page = (struct page *) memmap_start;
374 struct dev_pagemap *pgmap;
375
376 /*
377 * Uncoditionally retrieve a dev_pagemap associated with the
378 * given physical address, this is only for use in the
379 * arch_{add|remove}_memory() for setting up and tearing down
380 * the memmap.
381 */
382 rcu_read_lock();
383 pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
384 rcu_read_unlock();
385
386 return pgmap ? pgmap->altmap : NULL;
387}
388#endif /* CONFIG_SPARSEMEM_VMEMMAP */
41e94a85 389#endif /* CONFIG_ZONE_DEVICE */