1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/errno.h>
4 #include <linux/kernel.h>
6 #include <linux/memremap.h>
7 #include <linux/slab.h>
11 #include <xen/balloon.h>
15 static DEFINE_MUTEX(list_lock);
16 static struct page *page_list;
17 static unsigned int list_count;
19 static struct resource *target_resource;
22 * If arch is not happy with system "iomem_resource" being used for
23 * the region allocation it can provide it's own view by creating specific
24 * Xen resource with unused regions of guest physical address space provided
27 int __weak __init arch_xen_unpopulated_init(struct resource **res)
29 *res = &iomem_resource;
34 static int fill_list(unsigned int nr_pages)
36 struct dev_pagemap *pgmap;
37 struct resource *res, *tmp_res = NULL;
39 unsigned int i, alloc_pages = round_up(nr_pages, PAGES_PER_SECTION);
40 struct range mhp_range;
43 res = kzalloc(sizeof(*res), GFP_KERNEL);
47 res->name = "Xen scratch";
48 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
50 mhp_range = mhp_get_pluggable_range(true);
52 ret = allocate_resource(target_resource, res,
53 alloc_pages * PAGE_SIZE, mhp_range.start, mhp_range.end,
54 PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
56 pr_err("Cannot allocate new IOMEM resource\n");
61 * Reserve the region previously allocated from Xen resource to avoid
62 * re-using it by someone else.
64 if (target_resource != &iomem_resource) {
65 tmp_res = kzalloc(sizeof(*tmp_res), GFP_KERNEL);
71 tmp_res->name = res->name;
72 tmp_res->start = res->start;
73 tmp_res->end = res->end;
74 tmp_res->flags = res->flags;
76 ret = request_resource(&iomem_resource, tmp_res);
78 pr_err("Cannot request resource %pR (%d)\n", tmp_res, ret);
84 pgmap = kzalloc(sizeof(*pgmap), GFP_KERNEL);
90 pgmap->type = MEMORY_DEVICE_GENERIC;
91 pgmap->range = (struct range) {
98 #ifdef CONFIG_XEN_HAVE_PVMMU
100 * memremap will build page tables for the new memory so
101 * the p2m must contain invalid entries so the correct
102 * non-present PTEs will be written.
104 * If a failure occurs, the original (identity) p2m entries
105 * are not restored since this region is now known not to
106 * conflict with any devices.
108 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
109 xen_pfn_t pfn = PFN_DOWN(res->start);
111 for (i = 0; i < alloc_pages; i++) {
112 if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
113 pr_warn("set_phys_to_machine() failed, no memory added\n");
121 vaddr = memremap_pages(pgmap, NUMA_NO_NODE);
123 pr_err("Cannot remap memory range\n");
124 ret = PTR_ERR(vaddr);
128 for (i = 0; i < alloc_pages; i++) {
129 struct page *pg = virt_to_page(vaddr + PAGE_SIZE * i);
131 pg->zone_device_data = page_list;
142 release_resource(tmp_res);
146 release_resource(res);
153 * xen_alloc_unpopulated_pages - alloc unpopulated pages
154 * @nr_pages: Number of pages
155 * @pages: pages returned
156 * @return 0 on success, error otherwise
158 int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages)
164 * Fallback to default behavior if we do not have any suitable resource
165 * to allocate required region from and as the result we won't be able to
168 if (!target_resource)
169 return xen_alloc_ballooned_pages(nr_pages, pages);
171 mutex_lock(&list_lock);
172 if (list_count < nr_pages) {
173 ret = fill_list(nr_pages - list_count);
178 for (i = 0; i < nr_pages; i++) {
179 struct page *pg = page_list;
182 page_list = pg->zone_device_data;
186 #ifdef CONFIG_XEN_HAVE_PVMMU
187 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
188 ret = xen_alloc_p2m_entry(page_to_pfn(pg));
192 for (j = 0; j <= i; j++) {
193 pages[j]->zone_device_data = page_list;
194 page_list = pages[j];
204 mutex_unlock(&list_lock);
207 EXPORT_SYMBOL(xen_alloc_unpopulated_pages);
210 * xen_free_unpopulated_pages - return unpopulated pages
211 * @nr_pages: Number of pages
212 * @pages: pages to return
214 void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages)
218 if (!target_resource) {
219 xen_free_ballooned_pages(nr_pages, pages);
223 mutex_lock(&list_lock);
224 for (i = 0; i < nr_pages; i++) {
225 pages[i]->zone_device_data = page_list;
226 page_list = pages[i];
229 mutex_unlock(&list_lock);
231 EXPORT_SYMBOL(xen_free_unpopulated_pages);
233 static int __init unpopulated_init(void)
240 ret = arch_xen_unpopulated_init(&target_resource);
242 pr_err("xen:unpopulated: Cannot initialize target resource\n");
243 target_resource = NULL;
248 early_initcall(unpopulated_init);