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); |
| 139 | } else |
| 140 | devres_free(ptr); |
| 141 | |
| 142 | return addr; |
| 143 | } |
| 144 | EXPORT_SYMBOL(devm_memremap); |
| 145 | |
| 146 | void devm_memunmap(struct device *dev, void *addr) |
| 147 | { |
Dan Williams | d741314 | 2015-09-15 02:37:48 -0400 | [diff] [blame] | 148 | WARN_ON(devres_release(dev, devm_memremap_release, |
| 149 | devm_memremap_match, addr)); |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame] | 150 | } |
| 151 | EXPORT_SYMBOL(devm_memunmap); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 152 | |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 153 | pfn_t phys_to_pfn_t(dma_addr_t addr, unsigned long flags) |
| 154 | { |
| 155 | return __pfn_to_pfn_t(addr >> PAGE_SHIFT, flags); |
| 156 | } |
| 157 | EXPORT_SYMBOL(phys_to_pfn_t); |
| 158 | |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 159 | #ifdef CONFIG_ZONE_DEVICE |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 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) |
| 164 | |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 165 | struct page_map { |
| 166 | struct resource res; |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 167 | struct percpu_ref *ref; |
| 168 | struct dev_pagemap pgmap; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 169 | struct vmem_altmap altmap; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 172 | static void pgmap_radix_release(struct resource *res) |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 173 | { |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 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 | |
| 182 | static void devm_memremap_pages_release(struct device *dev, void *data) |
| 183 | { |
| 184 | struct page_map *page_map = data; |
| 185 | struct resource *res = &page_map->res; |
| 186 | resource_size_t align_start, align_size; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 187 | struct dev_pagemap *pgmap = &page_map->pgmap; |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 188 | |
| 189 | pgmap_radix_release(res); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 190 | |
| 191 | /* pages are dead and unused, undo the arch mapping */ |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 192 | align_start = res->start & ~(SECTION_SIZE - 1); |
| 193 | align_size = ALIGN(resource_size(res), SECTION_SIZE); |
| 194 | arch_remove_memory(align_start, align_size); |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 195 | dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc, |
| 196 | "%s: failed to free all reserved pages\n", __func__); |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* assumes rcu_read_lock() held at entry */ |
| 200 | struct dev_pagemap *find_dev_pagemap(resource_size_t phys) |
| 201 | { |
| 202 | struct page_map *page_map; |
| 203 | |
| 204 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 205 | |
| 206 | page_map = radix_tree_lookup(&pgmap_radix, phys >> PA_SECTION_SHIFT); |
| 207 | return page_map ? &page_map->pgmap : NULL; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 210 | /** |
| 211 | * devm_memremap_pages - remap and provide memmap backing for the given resource |
| 212 | * @dev: hosting device for @res |
| 213 | * @res: "host memory" address range |
| 214 | * @altmap: optional descriptor for allocating the memmap from @res |
| 215 | * |
| 216 | * Note, the expectation is that @res is a host memory range that could |
| 217 | * feasibly be treated as a "System RAM" range, i.e. not a device mmio |
| 218 | * range, but this is not enforced. |
| 219 | */ |
| 220 | void *devm_memremap_pages(struct device *dev, struct resource *res, |
| 221 | struct vmem_altmap *altmap) |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 222 | { |
| 223 | int is_ram = region_intersects(res->start, resource_size(res), |
| 224 | "System RAM"); |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 225 | resource_size_t key, align_start, align_size; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 226 | struct dev_pagemap *pgmap; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 227 | struct page_map *page_map; |
| 228 | int error, nid; |
| 229 | |
| 230 | if (is_ram == REGION_MIXED) { |
| 231 | WARN_ONCE(1, "%s attempted on mixed region %pr\n", |
| 232 | __func__, res); |
| 233 | return ERR_PTR(-ENXIO); |
| 234 | } |
| 235 | |
| 236 | if (is_ram == REGION_INTERSECTS) |
| 237 | return __va(res->start); |
| 238 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 239 | if (altmap && !IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) { |
| 240 | dev_err(dev, "%s: altmap requires CONFIG_SPARSEMEM_VMEMMAP=y\n", |
| 241 | __func__); |
| 242 | return ERR_PTR(-ENXIO); |
| 243 | } |
| 244 | |
Dan Williams | 538ea4a | 2015-10-05 20:35:56 -0400 | [diff] [blame] | 245 | page_map = devres_alloc_node(devm_memremap_pages_release, |
| 246 | sizeof(*page_map), GFP_KERNEL, dev_to_node(dev)); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 247 | if (!page_map) |
| 248 | return ERR_PTR(-ENOMEM); |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 249 | pgmap = &page_map->pgmap; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 250 | |
| 251 | memcpy(&page_map->res, res, sizeof(*res)); |
| 252 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 253 | pgmap->dev = dev; |
| 254 | if (altmap) { |
| 255 | memcpy(&page_map->altmap, altmap, sizeof(*altmap)); |
| 256 | pgmap->altmap = &page_map->altmap; |
| 257 | } |
| 258 | pgmap->res = &page_map->res; |
| 259 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 260 | mutex_lock(&pgmap_lock); |
| 261 | error = 0; |
| 262 | for (key = res->start; key <= res->end; key += SECTION_SIZE) { |
| 263 | struct dev_pagemap *dup; |
| 264 | |
| 265 | rcu_read_lock(); |
| 266 | dup = find_dev_pagemap(key); |
| 267 | rcu_read_unlock(); |
| 268 | if (dup) { |
| 269 | dev_err(dev, "%s: %pr collides with mapping for %s\n", |
| 270 | __func__, res, dev_name(dup->dev)); |
| 271 | error = -EBUSY; |
| 272 | break; |
| 273 | } |
| 274 | error = radix_tree_insert(&pgmap_radix, key >> PA_SECTION_SHIFT, |
| 275 | page_map); |
| 276 | if (error) { |
| 277 | dev_err(dev, "%s: failed: %d\n", __func__, error); |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | mutex_unlock(&pgmap_lock); |
| 282 | if (error) |
| 283 | goto err_radix; |
| 284 | |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 285 | nid = dev_to_node(dev); |
| 286 | if (nid < 0) |
Dan Williams | 7eff93b | 2015-10-05 20:35:55 -0400 | [diff] [blame] | 287 | nid = numa_mem_id(); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 288 | |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 289 | align_start = res->start & ~(SECTION_SIZE - 1); |
| 290 | align_size = ALIGN(resource_size(res), SECTION_SIZE); |
| 291 | error = arch_add_memory(nid, align_start, align_size, true); |
| 292 | if (error) |
| 293 | goto err_add_memory; |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 294 | |
| 295 | devres_add(dev, page_map); |
| 296 | return __va(res->start); |
Dan Williams | 9476df7 | 2016-01-15 16:56:19 -0800 | [diff] [blame] | 297 | |
| 298 | err_add_memory: |
| 299 | err_radix: |
| 300 | pgmap_radix_release(res); |
| 301 | devres_free(page_map); |
| 302 | return ERR_PTR(error); |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 303 | } |
| 304 | EXPORT_SYMBOL(devm_memremap_pages); |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame^] | 305 | |
| 306 | unsigned long vmem_altmap_offset(struct vmem_altmap *altmap) |
| 307 | { |
| 308 | /* number of pfns from base where pfn_to_page() is valid */ |
| 309 | return altmap->reserve + altmap->free; |
| 310 | } |
| 311 | |
| 312 | void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns) |
| 313 | { |
| 314 | altmap->alloc -= nr_pfns; |
| 315 | } |
| 316 | |
| 317 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 318 | struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start) |
| 319 | { |
| 320 | /* |
| 321 | * 'memmap_start' is the virtual address for the first "struct |
| 322 | * page" in this range of the vmemmap array. In the case of |
| 323 | * CONFIG_SPARSE_VMEMMAP a page_to_pfn conversion is simple |
| 324 | * pointer arithmetic, so we can perform this to_vmem_altmap() |
| 325 | * conversion without concern for the initialization state of |
| 326 | * the struct page fields. |
| 327 | */ |
| 328 | struct page *page = (struct page *) memmap_start; |
| 329 | struct dev_pagemap *pgmap; |
| 330 | |
| 331 | /* |
| 332 | * Uncoditionally retrieve a dev_pagemap associated with the |
| 333 | * given physical address, this is only for use in the |
| 334 | * arch_{add|remove}_memory() for setting up and tearing down |
| 335 | * the memmap. |
| 336 | */ |
| 337 | rcu_read_lock(); |
| 338 | pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page))); |
| 339 | rcu_read_unlock(); |
| 340 | |
| 341 | return pgmap ? pgmap->altmap : NULL; |
| 342 | } |
| 343 | #endif /* CONFIG_SPARSEMEM_VMEMMAP */ |
Christoph Hellwig | 41e94a8 | 2015-08-17 16:00:35 +0200 | [diff] [blame] | 344 | #endif /* CONFIG_ZONE_DEVICE */ |