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 | */ |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame^] | 13 | #include <linux/device.h> |
Dan Williams | 92281dee | 2015-08-10 23:07:06 -0400 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/mm.h> |
| 17 | |
| 18 | #ifndef ioremap_cache |
| 19 | /* temporary while we convert existing ioremap_cache users to memremap */ |
| 20 | __weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size) |
| 21 | { |
| 22 | return ioremap(offset, size); |
| 23 | } |
| 24 | #endif |
| 25 | |
| 26 | /** |
| 27 | * memremap() - remap an iomem_resource as cacheable memory |
| 28 | * @offset: iomem resource start address |
| 29 | * @size: size of remap |
| 30 | * @flags: either MEMREMAP_WB or MEMREMAP_WT |
| 31 | * |
| 32 | * memremap() is "ioremap" for cases where it is known that the resource |
| 33 | * being mapped does not have i/o side effects and the __iomem |
| 34 | * annotation is not applicable. |
| 35 | * |
| 36 | * MEMREMAP_WB - matches the default mapping for "System RAM" on |
| 37 | * the architecture. This is usually a read-allocate write-back cache. |
| 38 | * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM |
| 39 | * memremap() will bypass establishing a new mapping and instead return |
| 40 | * a pointer into the direct map. |
| 41 | * |
| 42 | * MEMREMAP_WT - establish a mapping whereby writes either bypass the |
| 43 | * cache or are written through to memory and never exist in a |
| 44 | * cache-dirty state with respect to program visibility. Attempts to |
| 45 | * map "System RAM" with this mapping type will fail. |
| 46 | */ |
| 47 | void *memremap(resource_size_t offset, size_t size, unsigned long flags) |
| 48 | { |
| 49 | int is_ram = region_intersects(offset, size, "System RAM"); |
| 50 | void *addr = NULL; |
| 51 | |
| 52 | if (is_ram == REGION_MIXED) { |
| 53 | WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n", |
| 54 | &offset, (unsigned long) size); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | /* Try all mapping types requested until one returns non-NULL */ |
| 59 | if (flags & MEMREMAP_WB) { |
| 60 | flags &= ~MEMREMAP_WB; |
| 61 | /* |
| 62 | * MEMREMAP_WB is special in that it can be satisifed |
| 63 | * from the direct map. Some archs depend on the |
| 64 | * capability of memremap() to autodetect cases where |
| 65 | * the requested range is potentially in "System RAM" |
| 66 | */ |
| 67 | if (is_ram == REGION_INTERSECTS) |
| 68 | addr = __va(offset); |
| 69 | else |
| 70 | addr = ioremap_cache(offset, size); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * If we don't have a mapping yet and more request flags are |
| 75 | * pending then we will be attempting to establish a new virtual |
| 76 | * address mapping. Enforce that this mapping is not aliasing |
| 77 | * "System RAM" |
| 78 | */ |
| 79 | if (!addr && is_ram == REGION_INTERSECTS && flags) { |
| 80 | WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n", |
| 81 | &offset, (unsigned long) size); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | if (!addr && (flags & MEMREMAP_WT)) { |
| 86 | flags &= ~MEMREMAP_WT; |
| 87 | addr = ioremap_wt(offset, size); |
| 88 | } |
| 89 | |
| 90 | return addr; |
| 91 | } |
| 92 | EXPORT_SYMBOL(memremap); |
| 93 | |
| 94 | void memunmap(void *addr) |
| 95 | { |
| 96 | if (is_vmalloc_addr(addr)) |
| 97 | iounmap((void __iomem *) addr); |
| 98 | } |
| 99 | EXPORT_SYMBOL(memunmap); |
Christoph Hellwig | 7d3dcf2 | 2015-08-10 23:07:07 -0400 | [diff] [blame^] | 100 | |
| 101 | static void devm_memremap_release(struct device *dev, void *res) |
| 102 | { |
| 103 | memunmap(res); |
| 104 | } |
| 105 | |
| 106 | static int devm_memremap_match(struct device *dev, void *res, void *match_data) |
| 107 | { |
| 108 | return *(void **)res == match_data; |
| 109 | } |
| 110 | |
| 111 | void *devm_memremap(struct device *dev, resource_size_t offset, |
| 112 | size_t size, unsigned long flags) |
| 113 | { |
| 114 | void **ptr, *addr; |
| 115 | |
| 116 | ptr = devres_alloc(devm_memremap_release, sizeof(*ptr), GFP_KERNEL); |
| 117 | if (!ptr) |
| 118 | return NULL; |
| 119 | |
| 120 | addr = memremap(offset, size, flags); |
| 121 | if (addr) { |
| 122 | *ptr = addr; |
| 123 | devres_add(dev, ptr); |
| 124 | } else |
| 125 | devres_free(ptr); |
| 126 | |
| 127 | return addr; |
| 128 | } |
| 129 | EXPORT_SYMBOL(devm_memremap); |
| 130 | |
| 131 | void devm_memunmap(struct device *dev, void *addr) |
| 132 | { |
| 133 | WARN_ON(devres_destroy(dev, devm_memremap_release, devm_memremap_match, |
| 134 | addr)); |
| 135 | memunmap(addr); |
| 136 | } |
| 137 | EXPORT_SYMBOL(devm_memunmap); |