Commit | Line | Data |
---|---|---|
5981690d DW |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | /* Copyright(c) 2015 Intel Corporation. All rights reserved. */ | |
9476df7d | 3 | #include <linux/radix-tree.h> |
7d3dcf26 | 4 | #include <linux/device.h> |
92281dee | 5 | #include <linux/types.h> |
34c0fd54 | 6 | #include <linux/pfn_t.h> |
92281dee DW |
7 | #include <linux/io.h> |
8 | #include <linux/mm.h> | |
41e94a85 | 9 | #include <linux/memory_hotplug.h> |
5042db43 JG |
10 | #include <linux/swap.h> |
11 | #include <linux/swapops.h> | |
92281dee | 12 | |
9476df7d DW |
13 | static DEFINE_MUTEX(pgmap_lock); |
14 | static RADIX_TREE(pgmap_radix, GFP_KERNEL); | |
15 | #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1) | |
16 | #define SECTION_SIZE (1UL << PA_SECTION_SHIFT) | |
17 | ||
ab1b597e | 18 | static unsigned long order_at(struct resource *res, unsigned long pgoff) |
9476df7d | 19 | { |
ab1b597e DW |
20 | unsigned long phys_pgoff = PHYS_PFN(res->start) + pgoff; |
21 | unsigned long nr_pages, mask; | |
eb7d78c9 | 22 | |
ab1b597e DW |
23 | nr_pages = PHYS_PFN(resource_size(res)); |
24 | if (nr_pages == pgoff) | |
25 | return ULONG_MAX; | |
26 | ||
27 | /* | |
28 | * What is the largest aligned power-of-2 range available from | |
29 | * this resource pgoff to the end of the resource range, | |
30 | * considering the alignment of the current pgoff? | |
31 | */ | |
32 | mask = phys_pgoff | rounddown_pow_of_two(nr_pages - pgoff); | |
33 | if (!mask) | |
34 | return ULONG_MAX; | |
35 | ||
36 | return find_first_bit(&mask, BITS_PER_LONG); | |
37 | } | |
38 | ||
39 | #define foreach_order_pgoff(res, order, pgoff) \ | |
40 | for (pgoff = 0, order = order_at((res), pgoff); order < ULONG_MAX; \ | |
41 | pgoff += 1UL << order, order = order_at((res), pgoff)) | |
42 | ||
5042db43 JG |
43 | #if IS_ENABLED(CONFIG_DEVICE_PRIVATE) |
44 | int device_private_entry_fault(struct vm_area_struct *vma, | |
45 | unsigned long addr, | |
46 | swp_entry_t entry, | |
47 | unsigned int flags, | |
48 | pmd_t *pmdp) | |
49 | { | |
50 | struct page *page = device_private_entry_to_page(entry); | |
51 | ||
52 | /* | |
53 | * The page_fault() callback must migrate page back to system memory | |
54 | * so that CPU can access it. This might fail for various reasons | |
55 | * (device issue, device was unsafely unplugged, ...). When such | |
56 | * error conditions happen, the callback must return VM_FAULT_SIGBUS. | |
57 | * | |
58 | * Note that because memory cgroup charges are accounted to the device | |
59 | * memory, this should never fail because of memory restrictions (but | |
60 | * allocation of regular system page might still fail because we are | |
61 | * out of memory). | |
62 | * | |
63 | * There is a more in-depth description of what that callback can and | |
64 | * cannot do, in include/linux/memremap.h | |
65 | */ | |
66 | return page->pgmap->page_fault(vma, addr, page, flags, pmdp); | |
67 | } | |
68 | EXPORT_SYMBOL(device_private_entry_fault); | |
69 | #endif /* CONFIG_DEVICE_PRIVATE */ | |
70 | ||
77dd66a3 | 71 | static void pgmap_radix_release(struct resource *res, unsigned long end_pgoff) |
ab1b597e DW |
72 | { |
73 | unsigned long pgoff, order; | |
9476df7d DW |
74 | |
75 | mutex_lock(&pgmap_lock); | |
77dd66a3 JS |
76 | foreach_order_pgoff(res, order, pgoff) { |
77 | if (pgoff >= end_pgoff) | |
78 | break; | |
ab1b597e | 79 | radix_tree_delete(&pgmap_radix, PHYS_PFN(res->start) + pgoff); |
77dd66a3 | 80 | } |
9476df7d | 81 | mutex_unlock(&pgmap_lock); |
ab1b597e DW |
82 | |
83 | synchronize_rcu(); | |
9476df7d DW |
84 | } |
85 | ||
e7744aa2 | 86 | static unsigned long pfn_first(struct dev_pagemap *pgmap) |
5c2c2587 | 87 | { |
e7744aa2 LG |
88 | const struct resource *res = &pgmap->res; |
89 | struct vmem_altmap *altmap = &pgmap->altmap; | |
5c2c2587 DW |
90 | unsigned long pfn; |
91 | ||
92 | pfn = res->start >> PAGE_SHIFT; | |
e7744aa2 | 93 | if (pgmap->altmap_valid) |
5c2c2587 DW |
94 | pfn += vmem_altmap_offset(altmap); |
95 | return pfn; | |
96 | } | |
97 | ||
e7744aa2 | 98 | static unsigned long pfn_end(struct dev_pagemap *pgmap) |
5c2c2587 | 99 | { |
e7744aa2 | 100 | const struct resource *res = &pgmap->res; |
5c2c2587 DW |
101 | |
102 | return (res->start + resource_size(res)) >> PAGE_SHIFT; | |
103 | } | |
104 | ||
949b9325 DW |
105 | static unsigned long pfn_next(unsigned long pfn) |
106 | { | |
107 | if (pfn % 1024 == 0) | |
108 | cond_resched(); | |
109 | return pfn + 1; | |
110 | } | |
111 | ||
5c2c2587 | 112 | #define for_each_device_pfn(pfn, map) \ |
949b9325 | 113 | for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn)) |
5c2c2587 | 114 | |
e8d51348 | 115 | static void devm_memremap_pages_release(void *data) |
41e94a85 | 116 | { |
e7744aa2 | 117 | struct dev_pagemap *pgmap = data; |
e8d51348 | 118 | struct device *dev = pgmap->dev; |
e7744aa2 | 119 | struct resource *res = &pgmap->res; |
9476df7d | 120 | resource_size_t align_start, align_size; |
71389703 DW |
121 | unsigned long pfn; |
122 | ||
e7744aa2 | 123 | for_each_device_pfn(pfn, pgmap) |
71389703 | 124 | put_page(pfn_to_page(pfn)); |
9476df7d | 125 | |
5c2c2587 DW |
126 | if (percpu_ref_tryget_live(pgmap->ref)) { |
127 | dev_WARN(dev, "%s: page mapping is still live!\n", __func__); | |
128 | percpu_ref_put(pgmap->ref); | |
129 | } | |
130 | ||
41e94a85 | 131 | /* pages are dead and unused, undo the arch mapping */ |
9476df7d | 132 | align_start = res->start & ~(SECTION_SIZE - 1); |
10a0cd6e JS |
133 | align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE) |
134 | - align_start; | |
b5d24fda | 135 | |
f931ab47 | 136 | mem_hotplug_begin(); |
e7744aa2 LG |
137 | arch_remove_memory(align_start, align_size, pgmap->altmap_valid ? |
138 | &pgmap->altmap : NULL); | |
f931ab47 | 139 | mem_hotplug_done(); |
b5d24fda | 140 | |
9049771f | 141 | untrack_pfn(NULL, PHYS_PFN(align_start), align_size); |
77dd66a3 | 142 | pgmap_radix_release(res, -1); |
e7744aa2 LG |
143 | dev_WARN_ONCE(dev, pgmap->altmap.alloc, |
144 | "%s: failed to free all reserved pages\n", __func__); | |
9476df7d DW |
145 | } |
146 | ||
4b94ffdc DW |
147 | /** |
148 | * devm_memremap_pages - remap and provide memmap backing for the given resource | |
149 | * @dev: hosting device for @res | |
e8d51348 | 150 | * @pgmap: pointer to a struct dev_pgmap |
4b94ffdc | 151 | * |
5c2c2587 | 152 | * Notes: |
e8d51348 CH |
153 | * 1/ At a minimum the res, ref and type members of @pgmap must be initialized |
154 | * by the caller before passing it to this function | |
155 | * | |
156 | * 2/ The altmap field may optionally be initialized, in which case altmap_valid | |
157 | * must be set to true | |
158 | * | |
159 | * 3/ pgmap.ref must be 'live' on entry and 'dead' before devm_memunmap_pages() | |
160 | * time (or devm release event). The expected order of events is that ref has | |
71389703 DW |
161 | * been through percpu_ref_kill() before devm_memremap_pages_release(). The |
162 | * wait for the completion of all references being dropped and | |
163 | * percpu_ref_exit() must occur after devm_memremap_pages_release(). | |
5c2c2587 | 164 | * |
e8d51348 | 165 | * 4/ res is expected to be a host memory range that could feasibly be |
5c2c2587 DW |
166 | * treated as a "System RAM" range, i.e. not a device mmio range, but |
167 | * this is not enforced. | |
4b94ffdc | 168 | */ |
e8d51348 | 169 | void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) |
41e94a85 | 170 | { |
ab1b597e | 171 | resource_size_t align_start, align_size, align_end; |
e8d51348 CH |
172 | struct vmem_altmap *altmap = pgmap->altmap_valid ? |
173 | &pgmap->altmap : NULL; | |
949b9325 | 174 | struct resource *res = &pgmap->res; |
ab1b597e | 175 | unsigned long pfn, pgoff, order; |
9049771f | 176 | pgprot_t pgprot = PAGE_KERNEL; |
949b9325 | 177 | int error, nid, is_ram; |
5f29a77c DW |
178 | |
179 | align_start = res->start & ~(SECTION_SIZE - 1); | |
180 | align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE) | |
181 | - align_start; | |
d37a14bb LT |
182 | is_ram = region_intersects(align_start, align_size, |
183 | IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE); | |
41e94a85 CH |
184 | |
185 | if (is_ram == REGION_MIXED) { | |
186 | WARN_ONCE(1, "%s attempted on mixed region %pr\n", | |
187 | __func__, res); | |
188 | return ERR_PTR(-ENXIO); | |
189 | } | |
190 | ||
191 | if (is_ram == REGION_INTERSECTS) | |
192 | return __va(res->start); | |
193 | ||
e8d51348 | 194 | if (!pgmap->ref) |
5c2c2587 DW |
195 | return ERR_PTR(-EINVAL); |
196 | ||
4b94ffdc | 197 | pgmap->dev = dev; |
4b94ffdc | 198 | |
9476df7d DW |
199 | mutex_lock(&pgmap_lock); |
200 | error = 0; | |
eb7d78c9 | 201 | align_end = align_start + align_size - 1; |
ab1b597e DW |
202 | |
203 | foreach_order_pgoff(res, order, pgoff) { | |
ab1b597e | 204 | error = __radix_tree_insert(&pgmap_radix, |
e7744aa2 | 205 | PHYS_PFN(res->start) + pgoff, order, pgmap); |
9476df7d DW |
206 | if (error) { |
207 | dev_err(dev, "%s: failed: %d\n", __func__, error); | |
208 | break; | |
209 | } | |
210 | } | |
211 | mutex_unlock(&pgmap_lock); | |
212 | if (error) | |
213 | goto err_radix; | |
214 | ||
41e94a85 CH |
215 | nid = dev_to_node(dev); |
216 | if (nid < 0) | |
7eff93b7 | 217 | nid = numa_mem_id(); |
41e94a85 | 218 | |
9049771f DW |
219 | error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0, |
220 | align_size); | |
221 | if (error) | |
222 | goto err_pfn_remap; | |
223 | ||
f931ab47 | 224 | mem_hotplug_begin(); |
24e6d5a5 | 225 | error = arch_add_memory(nid, align_start, align_size, altmap, false); |
f1dd2cd1 MH |
226 | if (!error) |
227 | move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE], | |
228 | align_start >> PAGE_SHIFT, | |
a99583e7 | 229 | align_size >> PAGE_SHIFT, altmap); |
f931ab47 | 230 | mem_hotplug_done(); |
9476df7d DW |
231 | if (error) |
232 | goto err_add_memory; | |
41e94a85 | 233 | |
e7744aa2 | 234 | for_each_device_pfn(pfn, pgmap) { |
5c2c2587 DW |
235 | struct page *page = pfn_to_page(pfn); |
236 | ||
d77a117e DW |
237 | /* |
238 | * ZONE_DEVICE pages union ->lru with a ->pgmap back | |
239 | * pointer. It is a bug if a ZONE_DEVICE page is ever | |
240 | * freed or placed on a driver-private list. Seed the | |
241 | * storage with LIST_POISON* values. | |
242 | */ | |
243 | list_del(&page->lru); | |
5c2c2587 | 244 | page->pgmap = pgmap; |
e8d51348 | 245 | percpu_ref_get(pgmap->ref); |
5c2c2587 | 246 | } |
e8d51348 CH |
247 | |
248 | devm_add_action(dev, devm_memremap_pages_release, pgmap); | |
249 | ||
41e94a85 | 250 | return __va(res->start); |
9476df7d DW |
251 | |
252 | err_add_memory: | |
9049771f DW |
253 | untrack_pfn(NULL, PHYS_PFN(align_start), align_size); |
254 | err_pfn_remap: | |
9476df7d | 255 | err_radix: |
77dd66a3 | 256 | pgmap_radix_release(res, pgoff); |
9476df7d | 257 | return ERR_PTR(error); |
41e94a85 CH |
258 | } |
259 | EXPORT_SYMBOL(devm_memremap_pages); | |
4b94ffdc DW |
260 | |
261 | unsigned long vmem_altmap_offset(struct vmem_altmap *altmap) | |
262 | { | |
263 | /* number of pfns from base where pfn_to_page() is valid */ | |
264 | return altmap->reserve + altmap->free; | |
265 | } | |
266 | ||
267 | void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns) | |
268 | { | |
269 | altmap->alloc -= nr_pfns; | |
270 | } | |
271 | ||
0822acb8 CH |
272 | /** |
273 | * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn | |
274 | * @pfn: page frame number to lookup page_map | |
275 | * @pgmap: optional known pgmap that already has a reference | |
276 | * | |
832d7aa0 CH |
277 | * If @pgmap is non-NULL and covers @pfn it will be returned as-is. If @pgmap |
278 | * is non-NULL but does not cover @pfn the reference to it will be released. | |
0822acb8 CH |
279 | */ |
280 | struct dev_pagemap *get_dev_pagemap(unsigned long pfn, | |
281 | struct dev_pagemap *pgmap) | |
282 | { | |
0822acb8 CH |
283 | resource_size_t phys = PFN_PHYS(pfn); |
284 | ||
285 | /* | |
832d7aa0 | 286 | * In the cached case we're already holding a live reference. |
0822acb8 | 287 | */ |
832d7aa0 | 288 | if (pgmap) { |
e7744aa2 | 289 | if (phys >= pgmap->res.start && phys <= pgmap->res.end) |
832d7aa0 CH |
290 | return pgmap; |
291 | put_dev_pagemap(pgmap); | |
0822acb8 CH |
292 | } |
293 | ||
294 | /* fall back to slow path lookup */ | |
295 | rcu_read_lock(); | |
e697c5b9 | 296 | pgmap = radix_tree_lookup(&pgmap_radix, PHYS_PFN(phys)); |
0822acb8 CH |
297 | if (pgmap && !percpu_ref_tryget_live(pgmap->ref)) |
298 | pgmap = NULL; | |
299 | rcu_read_unlock(); | |
300 | ||
301 | return pgmap; | |
302 | } | |
7b2d55d2 | 303 | |
df6ad698 JG |
304 | #if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC) |
305 | void put_zone_device_private_or_public_page(struct page *page) | |
7b2d55d2 JG |
306 | { |
307 | int count = page_ref_dec_return(page); | |
308 | ||
309 | /* | |
310 | * If refcount is 1 then page is freed and refcount is stable as nobody | |
311 | * holds a reference on the page. | |
312 | */ | |
313 | if (count == 1) { | |
314 | /* Clear Active bit in case of parallel mark_page_accessed */ | |
315 | __ClearPageActive(page); | |
316 | __ClearPageWaiters(page); | |
317 | ||
318 | page->mapping = NULL; | |
c733a828 | 319 | mem_cgroup_uncharge(page); |
7b2d55d2 JG |
320 | |
321 | page->pgmap->page_free(page, page->pgmap->data); | |
322 | } else if (!count) | |
323 | __put_page(page); | |
324 | } | |
df6ad698 JG |
325 | EXPORT_SYMBOL(put_zone_device_private_or_public_page); |
326 | #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */ |