blob: 97b31c774274f5009be2facaf4ffb37928944fb0 [file] [log] [blame]
Dan Williams92281dee2015-08-10 23:07:06 -04001/*
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 */
Dan Williams9476df72016-01-15 16:56:19 -080013#include <linux/radix-tree.h>
14#include <linux/memremap.h>
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -040015#include <linux/device.h>
Dan Williams92281dee2015-08-10 23:07:06 -040016#include <linux/types.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080017#include <linux/pfn_t.h>
Dan Williams92281dee2015-08-10 23:07:06 -040018#include <linux/io.h>
19#include <linux/mm.h>
Christoph Hellwig41e94a82015-08-17 16:00:35 +020020#include <linux/memory_hotplug.h>
Dan Williams92281dee2015-08-10 23:07:06 -040021
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
Dan Williams182475b2015-10-26 16:55:56 -040030static 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
Dan Williams92281dee2015-08-10 23:07:06 -040040/**
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 *
Toshi Kani1c29f252016-01-26 21:57:28 +010050 * MEMREMAP_WB - matches the default mapping for System RAM on
Dan Williams92281dee2015-08-10 23:07:06 -040051 * 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
Toshi Kani1c29f252016-01-26 21:57:28 +010059 * map System RAM with this mapping type will fail.
Dan Williams92281dee2015-08-10 23:07:06 -040060 */
61void *memremap(resource_size_t offset, size_t size, unsigned long flags)
62{
Toshi Kani1c29f252016-01-26 21:57:28 +010063 int is_ram = region_intersects(offset, size,
64 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
Dan Williams92281dee2015-08-10 23:07:06 -040065 void *addr = NULL;
66
67 if (is_ram == REGION_MIXED) {
68 WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
69 &offset, (unsigned long) size);
70 return NULL;
71 }
72
73 /* Try all mapping types requested until one returns non-NULL */
74 if (flags & MEMREMAP_WB) {
75 flags &= ~MEMREMAP_WB;
76 /*
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
Toshi Kani1c29f252016-01-26 21:57:28 +010080 * the requested range is potentially in System RAM.
Dan Williams92281dee2015-08-10 23:07:06 -040081 */
82 if (is_ram == REGION_INTERSECTS)
Dan Williams182475b2015-10-26 16:55:56 -040083 addr = try_ram_remap(offset, size);
84 if (!addr)
Dan Williams92281dee2015-08-10 23:07:06 -040085 addr = ioremap_cache(offset, size);
86 }
87
88 /*
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
Toshi Kani1c29f252016-01-26 21:57:28 +010092 * System RAM.
Dan Williams92281dee2015-08-10 23:07:06 -040093 */
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);
97 return NULL;
98 }
99
100 if (!addr && (flags & MEMREMAP_WT)) {
101 flags &= ~MEMREMAP_WT;
102 addr = ioremap_wt(offset, size);
103 }
104
105 return addr;
106}
107EXPORT_SYMBOL(memremap);
108
109void memunmap(void *addr)
110{
111 if (is_vmalloc_addr(addr))
112 iounmap((void __iomem *) addr);
113}
114EXPORT_SYMBOL(memunmap);
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -0400115
116static void devm_memremap_release(struct device *dev, void *res)
117{
Toshi Kani9273a8b2016-02-17 13:11:29 -0800118 memunmap(*(void **)res);
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -0400119}
120
121static int devm_memremap_match(struct device *dev, void *res, void *match_data)
122{
123 return *(void **)res == match_data;
124}
125
126void *devm_memremap(struct device *dev, resource_size_t offset,
127 size_t size, unsigned long flags)
128{
129 void **ptr, *addr;
130
Dan Williams538ea4a2015-10-05 20:35:56 -0400131 ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
132 dev_to_node(dev));
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -0400133 if (!ptr)
Dan Williamsb36f4762015-09-15 02:42:20 -0400134 return ERR_PTR(-ENOMEM);
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -0400135
136 addr = memremap(offset, size, flags);
137 if (addr) {
138 *ptr = addr;
139 devres_add(dev, ptr);
Toshi Kani93f834d2016-02-20 14:32:24 -0800140 } else {
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -0400141 devres_free(ptr);
Toshi Kani93f834d2016-02-20 14:32:24 -0800142 return ERR_PTR(-ENXIO);
143 }
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -0400144
145 return addr;
146}
147EXPORT_SYMBOL(devm_memremap);
148
149void devm_memunmap(struct device *dev, void *addr)
150{
Dan Williamsd7413142015-09-15 02:37:48 -0400151 WARN_ON(devres_release(dev, devm_memremap_release,
152 devm_memremap_match, addr));
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -0400153}
154EXPORT_SYMBOL(devm_memunmap);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200155
Dan Williamsdb78c222016-02-11 16:13:17 -0800156pfn_t phys_to_pfn_t(phys_addr_t addr, u64 flags)
Dan Williams34c0fd52016-01-15 16:56:14 -0800157{
158 return __pfn_to_pfn_t(addr >> PAGE_SHIFT, flags);
159}
160EXPORT_SYMBOL(phys_to_pfn_t);
161
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200162#ifdef CONFIG_ZONE_DEVICE
Dan Williams9476df72016-01-15 16:56:19 -0800163static DEFINE_MUTEX(pgmap_lock);
164static RADIX_TREE(pgmap_radix, GFP_KERNEL);
165#define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
166#define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
167
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200168struct page_map {
169 struct resource res;
Dan Williams9476df72016-01-15 16:56:19 -0800170 struct percpu_ref *ref;
171 struct dev_pagemap pgmap;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800172 struct vmem_altmap altmap;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200173};
174
Dan Williams3565fce2016-01-15 16:56:55 -0800175void get_zone_device_page(struct page *page)
176{
177 percpu_ref_get(page->pgmap->ref);
178}
179EXPORT_SYMBOL(get_zone_device_page);
180
181void put_zone_device_page(struct page *page)
182{
183 put_dev_pagemap(page->pgmap);
184}
185EXPORT_SYMBOL(put_zone_device_page);
186
Dan Williams9476df72016-01-15 16:56:19 -0800187static void pgmap_radix_release(struct resource *res)
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200188{
Dan Williamseb7d78c2016-01-29 21:48:34 -0800189 resource_size_t key, align_start, align_size, align_end;
190
191 align_start = res->start & ~(SECTION_SIZE - 1);
192 align_size = ALIGN(resource_size(res), SECTION_SIZE);
193 align_end = align_start + align_size - 1;
Dan Williams9476df72016-01-15 16:56:19 -0800194
195 mutex_lock(&pgmap_lock);
196 for (key = res->start; key <= res->end; key += SECTION_SIZE)
197 radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT);
198 mutex_unlock(&pgmap_lock);
199}
200
Dan Williams5c2c2582016-01-15 16:56:49 -0800201static unsigned long pfn_first(struct page_map *page_map)
202{
203 struct dev_pagemap *pgmap = &page_map->pgmap;
204 const struct resource *res = &page_map->res;
205 struct vmem_altmap *altmap = pgmap->altmap;
206 unsigned long pfn;
207
208 pfn = res->start >> PAGE_SHIFT;
209 if (altmap)
210 pfn += vmem_altmap_offset(altmap);
211 return pfn;
212}
213
214static unsigned long pfn_end(struct page_map *page_map)
215{
216 const struct resource *res = &page_map->res;
217
218 return (res->start + resource_size(res)) >> PAGE_SHIFT;
219}
220
221#define for_each_device_pfn(pfn, map) \
222 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++)
223
Dan Williams9476df72016-01-15 16:56:19 -0800224static void devm_memremap_pages_release(struct device *dev, void *data)
225{
226 struct page_map *page_map = data;
227 struct resource *res = &page_map->res;
228 resource_size_t align_start, align_size;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800229 struct dev_pagemap *pgmap = &page_map->pgmap;
Dan Williams9476df72016-01-15 16:56:19 -0800230
Dan Williams5c2c2582016-01-15 16:56:49 -0800231 if (percpu_ref_tryget_live(pgmap->ref)) {
232 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
233 percpu_ref_put(pgmap->ref);
234 }
235
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200236 /* pages are dead and unused, undo the arch mapping */
Dan Williams9476df72016-01-15 16:56:19 -0800237 align_start = res->start & ~(SECTION_SIZE - 1);
238 align_size = ALIGN(resource_size(res), SECTION_SIZE);
239 arch_remove_memory(align_start, align_size);
Dan Williamseb7d78c2016-01-29 21:48:34 -0800240 pgmap_radix_release(res);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800241 dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
242 "%s: failed to free all reserved pages\n", __func__);
Dan Williams9476df72016-01-15 16:56:19 -0800243}
244
245/* assumes rcu_read_lock() held at entry */
246struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
247{
248 struct page_map *page_map;
249
250 WARN_ON_ONCE(!rcu_read_lock_held());
251
252 page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT);
253 return page_map ? &page_map->pgmap : NULL;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200254}
255
Dan Williams4b94ffd2016-01-15 16:56:22 -0800256/**
257 * devm_memremap_pages - remap and provide memmap backing for the given resource
258 * @dev: hosting device for @res
259 * @res: "host memory" address range
Dan Williams5c2c2582016-01-15 16:56:49 -0800260 * @ref: a live per-cpu reference count
Dan Williams4b94ffd2016-01-15 16:56:22 -0800261 * @altmap: optional descriptor for allocating the memmap from @res
262 *
Dan Williams5c2c2582016-01-15 16:56:49 -0800263 * Notes:
264 * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
265 * (or devm release event).
266 *
267 * 2/ @res is expected to be a host memory range that could feasibly be
268 * treated as a "System RAM" range, i.e. not a device mmio range, but
269 * this is not enforced.
Dan Williams4b94ffd2016-01-15 16:56:22 -0800270 */
271void *devm_memremap_pages(struct device *dev, struct resource *res,
Dan Williams5c2c2582016-01-15 16:56:49 -0800272 struct percpu_ref *ref, struct vmem_altmap *altmap)
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200273{
274 int is_ram = region_intersects(res->start, resource_size(res),
Toshi Kani1c29f252016-01-26 21:57:28 +0100275 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
Dan Williamseb7d78c2016-01-29 21:48:34 -0800276 resource_size_t key, align_start, align_size, align_end;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800277 struct dev_pagemap *pgmap;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200278 struct page_map *page_map;
Dan Williams5c2c2582016-01-15 16:56:49 -0800279 unsigned long pfn;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200280 int error, nid;
281
282 if (is_ram == REGION_MIXED) {
283 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
284 __func__, res);
285 return ERR_PTR(-ENXIO);
286 }
287
288 if (is_ram == REGION_INTERSECTS)
289 return __va(res->start);
290
Dan Williams4b94ffd2016-01-15 16:56:22 -0800291 if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) {
292 dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n",
293 __func__);
294 return ERR_PTR(-ENXIO);
295 }
296
Dan Williams5c2c2582016-01-15 16:56:49 -0800297 if (!ref)
298 return ERR_PTR(-EINVAL);
299
Dan Williams538ea4a2015-10-05 20:35:56 -0400300 page_map = devres_alloc_node(devm_memremap_pages_release,
301 sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200302 if (!page_map)
303 return ERR_PTR(-ENOMEM);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800304 pgmap = &page_map->pgmap;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200305
306 memcpy(&page_map->res, res, sizeof(*res));
307
Dan Williams4b94ffd2016-01-15 16:56:22 -0800308 pgmap->dev = dev;
309 if (altmap) {
310 memcpy(&page_map->altmap, altmap, sizeof(*altmap));
311 pgmap->altmap = &page_map->altmap;
312 }
Dan Williams5c2c2582016-01-15 16:56:49 -0800313 pgmap->ref = ref;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800314 pgmap->res = &page_map->res;
315
Dan Williams9476df72016-01-15 16:56:19 -0800316 mutex_lock(&pgmap_lock);
317 error = 0;
Dan Williamseb7d78c2016-01-29 21:48:34 -0800318 align_start = res->start & ~(SECTION_SIZE - 1);
319 align_size = ALIGN(resource_size(res), SECTION_SIZE);
320 align_end = align_start + align_size - 1;
321 for (key = align_start; key <= align_end; key += SECTION_SIZE) {
Dan Williams9476df72016-01-15 16:56:19 -0800322 struct dev_pagemap *dup;
323
324 rcu_read_lock();
325 dup = find_dev_pagemap(key);
326 rcu_read_unlock();
327 if (dup) {
328 dev_err(dev, "%s: %pr collides with mapping for %s\n",
329 __func__, res, dev_name(dup->dev));
330 error = -EBUSY;
331 break;
332 }
333 error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT,
334 page_map);
335 if (error) {
336 dev_err(dev, "%s: failed: %d\n", __func__, error);
337 break;
338 }
339 }
340 mutex_unlock(&pgmap_lock);
341 if (error)
342 goto err_radix;
343
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200344 nid = dev_to_node(dev);
345 if (nid < 0)
Dan Williams7eff93b2015-10-05 20:35:55 -0400346 nid = numa_mem_id();
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200347
Dan Williams9476df72016-01-15 16:56:19 -0800348 error = arch_add_memory(nid, align_start, align_size, true);
349 if (error)
350 goto err_add_memory;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200351
Dan Williams5c2c2582016-01-15 16:56:49 -0800352 for_each_device_pfn(pfn, page_map) {
353 struct page *page = pfn_to_page(pfn);
354
355 /* ZONE_DEVICE pages must never appear on a slab lru */
356 list_force_poison(&page->lru);
357 page->pgmap = pgmap;
358 }
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200359 devres_add(dev, page_map);
360 return __va(res->start);
Dan Williams9476df72016-01-15 16:56:19 -0800361
362 err_add_memory:
363 err_radix:
364 pgmap_radix_release(res);
365 devres_free(page_map);
366 return ERR_PTR(error);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200367}
368EXPORT_SYMBOL(devm_memremap_pages);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800369
370unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
371{
372 /* number of pfns from base where pfn_to_page() is valid */
373 return altmap->reserve + altmap->free;
374}
375
376void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
377{
378 altmap->alloc -= nr_pfns;
379}
380
381#ifdef CONFIG_SPARSEMEM_VMEMMAP
382struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
383{
384 /*
385 * 'memmap_start' is the virtual address for the first "struct
386 * page" in this range of the vmemmap array. In the case of
387 * CONFIG_SPARSE_VMEMMAP a page_to_pfn conversion is simple
388 * pointer arithmetic, so we can perform this to_vmem_altmap()
389 * conversion without concern for the initialization state of
390 * the struct page fields.
391 */
392 struct page *page = (struct page *) memmap_start;
393 struct dev_pagemap *pgmap;
394
395 /*
396 * Uncoditionally retrieve a dev_pagemap associated with the
397 * given physical address, this is only for use in the
398 * arch_{add|remove}_memory() for setting up and tearing down
399 * the memmap.
400 */
401 rcu_read_lock();
402 pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
403 rcu_read_unlock();
404
405 return pgmap ? pgmap->altmap : NULL;
406}
407#endif /* CONFIG_SPARSEMEM_VMEMMAP */
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200408#endif /* CONFIG_ZONE_DEVICE */