^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/memremap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <xen/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static DEFINE_MUTEX(list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static struct page *page_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static unsigned int list_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static int fill_list(unsigned int nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct dev_pagemap *pgmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) void *vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int i, alloc_pages = round_up(nr_pages, PAGES_PER_SECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) res = kzalloc(sizeof(*res), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) res->name = "Xen scratch";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ret = allocate_resource(&iomem_resource, res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) alloc_pages * PAGE_SIZE, 0, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) pr_err("Cannot allocate new IOMEM resource\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto err_resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) pgmap = kzalloc(sizeof(*pgmap), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!pgmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) goto err_pgmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) pgmap->type = MEMORY_DEVICE_GENERIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pgmap->range = (struct range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .start = res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .end = res->end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) pgmap->nr_range = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pgmap->owner = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #ifdef CONFIG_XEN_HAVE_PVMMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * memremap will build page tables for the new memory so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * the p2m must contain invalid entries so the correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * non-present PTEs will be written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * If a failure occurs, the original (identity) p2m entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * are not restored since this region is now known not to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * conflict with any devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!xen_feature(XENFEAT_auto_translated_physmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) xen_pfn_t pfn = PFN_DOWN(res->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) for (i = 0; i < alloc_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) pr_warn("set_phys_to_machine() failed, no memory added\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) goto err_memremap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) vaddr = memremap_pages(pgmap, NUMA_NO_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (IS_ERR(vaddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) pr_err("Cannot remap memory range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ret = PTR_ERR(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) goto err_memremap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) for (i = 0; i < alloc_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct page *pg = virt_to_page(vaddr + PAGE_SIZE * i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) BUG_ON(!virt_addr_valid(vaddr + PAGE_SIZE * i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pg->zone_device_data = page_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) page_list = pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) list_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) err_memremap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) kfree(pgmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) err_pgmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) release_resource(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err_resource:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) kfree(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * xen_alloc_unpopulated_pages - alloc unpopulated pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @nr_pages: Number of pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * @pages: pages returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * @return 0 on success, error otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) mutex_lock(&list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (list_count < nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ret = fill_list(nr_pages - list_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct page *pg = page_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) BUG_ON(!pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) page_list = pg->zone_device_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) list_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) pages[i] = pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #ifdef CONFIG_XEN_HAVE_PVMMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!xen_feature(XENFEAT_auto_translated_physmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = xen_alloc_p2m_entry(page_to_pfn(pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) for (j = 0; j <= i; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pages[j]->zone_device_data = page_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) page_list = pages[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) list_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) mutex_unlock(&list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) EXPORT_SYMBOL(xen_alloc_unpopulated_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * xen_free_unpopulated_pages - return unpopulated pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @nr_pages: Number of pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * @pages: pages to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) mutex_lock(&list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) pages[i]->zone_device_data = page_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) page_list = pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) list_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) mutex_unlock(&list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) EXPORT_SYMBOL(xen_free_unpopulated_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) #ifdef CONFIG_XEN_PV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int __init init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!xen_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!xen_pv_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * Initialize with pages from the extra memory regions (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * arch/x86/xen/setup.c).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) unsigned int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) for (j = 0; j < xen_extra_mem[i].n_pfns; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct page *pg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) pfn_to_page(xen_extra_mem[i].start_pfn + j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pg->zone_device_data = page_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) page_list = pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) list_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) subsys_initcall(init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) #endif