1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/memblock.h>
4 #include <linux/export.h>
5 #include <linux/spinlock.h>
6 #include <linux/slab.h>
7 #include <linux/types.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/vmalloc.h>
10 #include <linux/swiotlb.h>
13 #include <xen/interface/memory.h>
15 #include <xen/swiotlb-xen.h>
17 #include <asm/cacheflush.h>
18 #include <asm/xen/hypercall.h>
19 #include <asm/xen/interface.h>
21 struct xen_p2m_entry {
24 unsigned long nr_pages;
25 struct rb_node rbnode_phys;
28 static rwlock_t p2m_lock;
29 struct rb_root phys_to_mach = RB_ROOT;
30 EXPORT_SYMBOL_GPL(phys_to_mach);
32 static int xen_add_phys_to_mach_entry(struct xen_p2m_entry *new)
34 struct rb_node **link = &phys_to_mach.rb_node;
35 struct rb_node *parent = NULL;
36 struct xen_p2m_entry *entry;
41 entry = rb_entry(parent, struct xen_p2m_entry, rbnode_phys);
43 if (new->pfn == entry->pfn)
46 if (new->pfn < entry->pfn)
47 link = &(*link)->rb_left;
49 link = &(*link)->rb_right;
51 rb_link_node(&new->rbnode_phys, parent, link);
52 rb_insert_color(&new->rbnode_phys, &phys_to_mach);
57 pr_warn("%s: cannot add pfn=%pa -> mfn=%pa: pfn=%pa -> mfn=%pa already exists\n",
58 __func__, &new->pfn, &new->mfn, &entry->pfn, &entry->mfn);
63 unsigned long __pfn_to_mfn(unsigned long pfn)
66 struct xen_p2m_entry *entry;
67 unsigned long irqflags;
69 read_lock_irqsave(&p2m_lock, irqflags);
70 n = phys_to_mach.rb_node;
72 entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
73 if (entry->pfn <= pfn &&
74 entry->pfn + entry->nr_pages > pfn) {
75 unsigned long mfn = entry->mfn + (pfn - entry->pfn);
76 read_unlock_irqrestore(&p2m_lock, irqflags);
84 read_unlock_irqrestore(&p2m_lock, irqflags);
86 return INVALID_P2M_ENTRY;
88 EXPORT_SYMBOL_GPL(__pfn_to_mfn);
90 int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
91 struct gnttab_map_grant_ref *kmap_ops,
92 struct page **pages, unsigned int count)
96 for (i = 0; i < count; i++) {
97 struct gnttab_unmap_grant_ref unmap;
100 if (map_ops[i].status)
102 if (likely(set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT,
103 map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT)))
107 * Signal an error for this slot. This in turn requires
108 * immediate unmapping.
110 map_ops[i].status = GNTST_general_error;
111 unmap.host_addr = map_ops[i].host_addr,
112 unmap.handle = map_ops[i].handle;
113 map_ops[i].handle = ~0;
114 if (map_ops[i].flags & GNTMAP_device_map)
115 unmap.dev_bus_addr = map_ops[i].dev_bus_addr;
117 unmap.dev_bus_addr = 0;
120 * Pre-populate the status field, to be recognizable in
121 * the log message below.
125 rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
127 if (rc || unmap.status != GNTST_okay)
128 pr_err_once("gnttab unmap failed: rc=%d st=%d\n",
134 EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
136 int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
137 struct gnttab_unmap_grant_ref *kunmap_ops,
138 struct page **pages, unsigned int count)
142 for (i = 0; i < count; i++) {
143 set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
149 EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
151 bool __set_phys_to_machine_multi(unsigned long pfn,
152 unsigned long mfn, unsigned long nr_pages)
155 unsigned long irqflags;
156 struct xen_p2m_entry *p2m_entry;
159 if (mfn == INVALID_P2M_ENTRY) {
160 write_lock_irqsave(&p2m_lock, irqflags);
161 n = phys_to_mach.rb_node;
163 p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
164 if (p2m_entry->pfn <= pfn &&
165 p2m_entry->pfn + p2m_entry->nr_pages > pfn) {
166 rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach);
167 write_unlock_irqrestore(&p2m_lock, irqflags);
171 if (pfn < p2m_entry->pfn)
176 write_unlock_irqrestore(&p2m_lock, irqflags);
180 p2m_entry = kzalloc(sizeof(*p2m_entry), GFP_NOWAIT);
184 p2m_entry->pfn = pfn;
185 p2m_entry->nr_pages = nr_pages;
186 p2m_entry->mfn = mfn;
188 write_lock_irqsave(&p2m_lock, irqflags);
189 rc = xen_add_phys_to_mach_entry(p2m_entry);
191 write_unlock_irqrestore(&p2m_lock, irqflags);
195 write_unlock_irqrestore(&p2m_lock, irqflags);
198 EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
200 bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
202 return __set_phys_to_machine_multi(pfn, mfn, 1);
204 EXPORT_SYMBOL_GPL(__set_phys_to_machine);
206 static int p2m_init(void)
208 rwlock_init(&p2m_lock);
211 arch_initcall(p2m_init);