Dan Williams | 92281dee | 2015-08-10 23:07:06 -0400 | [diff] [blame] | 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 | */ |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 13 | #include <linux/radix-tree.h> |
| 14 | #include <linux/memremap.h> |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame] | 15 | #include <linux/device.h> |
Dan Williams | 92281dee | 2015-08-10 23:07:06 -0400 | [diff] [blame] | 16 | #include <linux/types.h> |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 17 | #include <linux/pfn_t.h> |
Dan Williams | 92281dee | 2015-08-10 23:07:06 -0400 | [diff] [blame] | 18 | #include <linux/io.h> |
| 19 | #include <linux/mm.h> |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 20 | #include <linux/memory_hotplug.h> |
Dan Williams | 92281dee | 2015-08-10 23:07:06 -0400 | [diff] [blame] | 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 | |
Dan Williams | 182475b | 2015-10-26 16:55:56 -0400 | [diff] [blame] | 30 | static 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 Williams | 92281dee | 2015-08-10 23:07:06 -0400 | [diff] [blame] | 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 | */ |
| 61 | void *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) |
Dan Williams | 182475b | 2015-10-26 16:55:56 -0400 | [diff] [blame] | 82 | addr = try_ram_remap(offset, size); |
| 83 | if (!addr) |
Dan Williams | 92281dee | 2015-08-10 23:07:06 -0400 | [diff] [blame] | 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 | } |
| 106 | EXPORT_SYMBOL(memremap); |
| 107 | |
| 108 | void memunmap(void *addr) |
| 109 | { |
| 110 | if (is_vmalloc_addr(addr)) |
| 111 | iounmap((void __iomem *) addr); |
| 112 | } |
| 113 | EXPORT_SYMBOL(memunmap); |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame] | 114 | |
| 115 | static void devm_memremap_release(struct device *dev, void *res) |
| 116 | { |
| 117 | memunmap(res); |
| 118 | } |
| 119 | |
| 120 | static int devm_memremap_match(struct device *dev, void *res, void *match_data) |
| 121 | { |
| 122 | return *(void **)res == match_data; |
| 123 | } |
| 124 | |
| 125 | void *devm_memremap(struct device *dev, resource_size_t offset, |
| 126 | size_t size, unsigned long flags) |
| 127 | { |
| 128 | void **ptr, *addr; |
| 129 | |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 130 | ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL, |
| 131 | dev_to_node(dev)); |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame] | 132 | if (!ptr) |
Dan Williams | b36f476 | 2015-09-15 02:42:20 -0400 | [diff] [blame] | 133 | return ERR_PTR(-ENOMEM); |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame] | 134 | |
| 135 | addr = memremap(offset, size, flags); |
| 136 | if (addr) { |
| 137 | *ptr = addr; |
| 138 | devres_add(dev, ptr); |
Toshi Kani | 93f834d | 2016-02-20 14:32:24 -0800 | [diff] [blame^] | 139 | } else { |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame] | 140 | devres_free(ptr); |
Toshi Kani | 93f834d | 2016-02-20 14:32:24 -0800 | [diff] [blame^] | 141 | return ERR_PTR(-ENXIO); |
| 142 | } |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame] | 143 | |
| 144 | return addr; |
| 145 | } |
| 146 | EXPORT_SYMBOL(devm_memremap); |
| 147 | |
| 148 | void devm_memunmap(struct device *dev, void *addr) |
| 149 | { |
Dan Williams | d741314 | 2015-09-15 02:37:48 -0400 | [diff] [blame] | 150 | WARN_ON(devres_release(dev, devm_memremap_release, |
| 151 | devm_memremap_match, addr)); |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame] | 152 | } |
| 153 | EXPORT_SYMBOL(devm_memunmap); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 154 | |
Dan Williams | db78c22 | 2016-02-11 16:13:17 -0800 | [diff] [blame] | 155 | pfn_t phys_to_pfn_t(phys_addr_t addr, u64 flags) |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 156 | { |
| 157 | return __pfn_to_pfn_t(addr >> PAGE_SHIFT, flags); |
| 158 | } |
| 159 | EXPORT_SYMBOL(phys_to_pfn_t); |
| 160 | |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 161 | #ifdef CONFIG_ZONE_DEVICE |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 162 | static DEFINE_MUTEX(pgmap_lock); |
| 163 | static RADIX_TREE(pgmap_radix, GFP_KERNEL); |
| 164 | #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1) |
| 165 | #define SECTION_SIZE (1UL << PA_SECTION_SHIFT) |
| 166 | |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 167 | struct page_map { |
| 168 | struct resource res; |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 169 | struct percpu_ref *ref; |
| 170 | struct dev_pagemap pgmap; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 171 | struct vmem_altmap altmap; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 172 | }; |
| 173 | |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 174 | void get_zone_device_page(struct page *page) |
| 175 | { |
| 176 | percpu_ref_get(page->pgmap->ref); |
| 177 | } |
| 178 | EXPORT_SYMBOL(get_zone_device_page); |
| 179 | |
| 180 | void put_zone_device_page(struct page *page) |
| 181 | { |
| 182 | put_dev_pagemap(page->pgmap); |
| 183 | } |
| 184 | EXPORT_SYMBOL(put_zone_device_page); |
| 185 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 186 | static void pgmap_radix_release(struct resource *res) |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 187 | { |
Dan Williams | eb7d78c | 2016-01-29 21:48:34 -0800 | [diff] [blame] | 188 | resource_size_t key, align_start, align_size, align_end; |
| 189 | |
| 190 | align_start = res->start & ~(SECTION_SIZE - 1); |
| 191 | align_size = ALIGN(resource_size(res), SECTION_SIZE); |
| 192 | align_end = align_start + align_size - 1; |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 193 | |
| 194 | mutex_lock(&pgmap_lock); |
| 195 | for (key = res->start; key <= res->end; key += SECTION_SIZE) |
| 196 | radix_tree_delete(&pgmap_radix, key >> PA_SECTION_SHIFT); |
| 197 | mutex_unlock(&pgmap_lock); |
| 198 | } |
| 199 | |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 200 | static unsigned long pfn_first(struct page_map *page_map) |
| 201 | { |
| 202 | struct dev_pagemap *pgmap = &page_map->pgmap; |
| 203 | const struct resource *res = &page_map->res; |
| 204 | struct vmem_altmap *altmap = pgmap->altmap; |
| 205 | unsigned long pfn; |
| 206 | |
| 207 | pfn = res->start >> PAGE_SHIFT; |
| 208 | if (altmap) |
| 209 | pfn += vmem_altmap_offset(altmap); |
| 210 | return pfn; |
| 211 | } |
| 212 | |
| 213 | static unsigned long pfn_end(struct page_map *page_map) |
| 214 | { |
| 215 | const struct resource *res = &page_map->res; |
| 216 | |
| 217 | return (res->start + resource_size(res)) >> PAGE_SHIFT; |
| 218 | } |
| 219 | |
| 220 | #define for_each_device_pfn(pfn, map) \ |
| 221 | for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++) |
| 222 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 223 | static void devm_memremap_pages_release(struct device *dev, void *data) |
| 224 | { |
| 225 | struct page_map *page_map = data; |
| 226 | struct resource *res = &page_map->res; |
| 227 | resource_size_t align_start, align_size; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 228 | struct dev_pagemap *pgmap = &page_map->pgmap; |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 229 | |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 230 | if (percpu_ref_tryget_live(pgmap->ref)) { |
| 231 | dev_WARN(dev, "%s: page mapping is still live!\n", __func__); |
| 232 | percpu_ref_put(pgmap->ref); |
| 233 | } |
| 234 | |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 235 | /* pages are dead and unused, undo the arch mapping */ |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 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); |
Dan Williams | eb7d78c | 2016-01-29 21:48:34 -0800 | [diff] [blame] | 239 | pgmap_radix_release(res); |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 240 | dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc, |
| 241 | "%s: failed to free all reserved pages\n", __func__); |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* assumes rcu_read_lock() held at entry */ |
| 245 | struct dev_pagemap *find_dev_pagemap(resource_size_t phys) |
| 246 | { |
| 247 | struct page_map *page_map; |
| 248 | |
| 249 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 250 | |
| 251 | page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT); |
| 252 | return page_map ? &page_map->pgmap : NULL; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 255 | /** |
| 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 |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 259 | * @ref: a live per-cpu reference count |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 260 | * @altmap: optional descriptor for allocating the memmap from @res |
| 261 | * |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 262 | * Notes: |
| 263 | * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time |
| 264 | * (or devm release event). |
| 265 | * |
| 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. |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 269 | */ |
| 270 | void *devm_memremap_pages(struct device *dev, struct resource *res, |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 271 | struct percpu_ref *ref, struct vmem_altmap *altmap) |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 272 | { |
| 273 | int is_ram = region_intersects(res->start, resource_size(res), |
| 274 | "System RAM"); |
Dan Williams | eb7d78c | 2016-01-29 21:48:34 -0800 | [diff] [blame] | 275 | resource_size_t key, align_start, align_size, align_end; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 276 | struct dev_pagemap *pgmap; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 277 | struct page_map *page_map; |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 278 | unsigned long pfn; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 279 | int error, nid; |
| 280 | |
| 281 | if (is_ram == REGION_MIXED) { |
| 282 | WARN_ONCE(1, "%s attempted on mixed region %pr\n", |
| 283 | __func__, res); |
| 284 | return ERR_PTR(-ENXIO); |
| 285 | } |
| 286 | |
| 287 | if (is_ram == REGION_INTERSECTS) |
| 288 | return __va(res->start); |
| 289 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 290 | if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) { |
| 291 | dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n", |
| 292 | __func__); |
| 293 | return ERR_PTR(-ENXIO); |
| 294 | } |
| 295 | |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 296 | if (!ref) |
| 297 | return ERR_PTR(-EINVAL); |
| 298 | |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 299 | page_map = devres_alloc_node(devm_memremap_pages_release, |
| 300 | sizeof(*page_map), GFP_KERNEL, dev_to_node(dev)); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 301 | if (!page_map) |
| 302 | return ERR_PTR(-ENOMEM); |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 303 | pgmap = &page_map->pgmap; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 304 | |
| 305 | memcpy(&page_map->res, res, sizeof(*res)); |
| 306 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 307 | pgmap->dev = dev; |
| 308 | if (altmap) { |
| 309 | memcpy(&page_map->altmap, altmap, sizeof(*altmap)); |
| 310 | pgmap->altmap = &page_map->altmap; |
| 311 | } |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 312 | pgmap->ref = ref; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 313 | pgmap->res = &page_map->res; |
| 314 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 315 | mutex_lock(&pgmap_lock); |
| 316 | error = 0; |
Dan Williams | eb7d78c | 2016-01-29 21:48:34 -0800 | [diff] [blame] | 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) { |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 321 | struct dev_pagemap *dup; |
| 322 | |
| 323 | rcu_read_lock(); |
| 324 | dup = find_dev_pagemap(key); |
| 325 | rcu_read_unlock(); |
| 326 | if (dup) { |
| 327 | dev_err(dev, "%s: %pr collides with mapping for %s\n", |
| 328 | __func__, res, dev_name(dup->dev)); |
| 329 | error = -EBUSY; |
| 330 | break; |
| 331 | } |
| 332 | error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT, |
| 333 | page_map); |
| 334 | if (error) { |
| 335 | dev_err(dev, "%s: failed: %d\n", __func__, error); |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | mutex_unlock(&pgmap_lock); |
| 340 | if (error) |
| 341 | goto err_radix; |
| 342 | |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 343 | nid = dev_to_node(dev); |
| 344 | if (nid < 0) |
Dan Williams | 7eff93b | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 345 | nid = numa_mem_id(); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 346 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 347 | error = arch_add_memory(nid, align_start, align_size, true); |
| 348 | if (error) |
| 349 | goto err_add_memory; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 350 | |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 351 | for_each_device_pfn(pfn, page_map) { |
| 352 | struct page *page = pfn_to_page(pfn); |
| 353 | |
| 354 | /* ZONE_DEVICE pages must never appear on a slab lru */ |
| 355 | list_force_poison(&page->lru); |
| 356 | page->pgmap = pgmap; |
| 357 | } |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 358 | devres_add(dev, page_map); |
| 359 | return __va(res->start); |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 360 | |
| 361 | err_add_memory: |
| 362 | err_radix: |
| 363 | pgmap_radix_release(res); |
| 364 | devres_free(page_map); |
| 365 | return ERR_PTR(error); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 366 | } |
| 367 | EXPORT_SYMBOL(devm_memremap_pages); |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 368 | |
| 369 | unsigned long vmem_altmap_offset(struct vmem_altmap *altmap) |
| 370 | { |
| 371 | /* number of pfns from base where pfn_to_page() is valid */ |
| 372 | return altmap->reserve + altmap->free; |
| 373 | } |
| 374 | |
| 375 | void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns) |
| 376 | { |
| 377 | altmap->alloc -= nr_pfns; |
| 378 | } |
| 379 | |
| 380 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 381 | struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start) |
| 382 | { |
| 383 | /* |
| 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. |
| 390 | */ |
| 391 | struct page *page = (struct page *) memmap_start; |
| 392 | struct dev_pagemap *pgmap; |
| 393 | |
| 394 | /* |
| 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 |
| 398 | * the memmap. |
| 399 | */ |
| 400 | rcu_read_lock(); |
| 401 | pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page))); |
| 402 | rcu_read_unlock(); |
| 403 | |
| 404 | return pgmap ? pgmap->altmap : NULL; |
| 405 | } |
| 406 | #endif /* CONFIG_SPARSEMEM_VMEMMAP */ |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 407 | #endif /* CONFIG_ZONE_DEVICE */ |