1 #include <linux/bootmem.h>
3 #include <linux/export.h>
4 #include <linux/spinlock.h>
5 #include <linux/slab.h>
6 #include <linux/types.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/vmalloc.h>
9 #include <linux/swiotlb.h>
12 #include <xen/interface/memory.h>
14 #include <xen/swiotlb-xen.h>
16 #include <asm/cacheflush.h>
17 #include <asm/xen/hypercall.h>
18 #include <asm/xen/interface.h>
20 struct xen_p2m_entry {
23 unsigned long nr_pages;
24 struct rb_node rbnode_phys;
27 static rwlock_t p2m_lock;
28 struct rb_root phys_to_mach = RB_ROOT;
29 EXPORT_SYMBOL_GPL(phys_to_mach);
31 static int xen_add_phys_to_mach_entry(struct xen_p2m_entry *new)
33 struct rb_node **link = &phys_to_mach.rb_node;
34 struct rb_node *parent = NULL;
35 struct xen_p2m_entry *entry;
40 entry = rb_entry(parent, struct xen_p2m_entry, rbnode_phys);
42 if (new->pfn == entry->pfn)
45 if (new->pfn < entry->pfn)
46 link = &(*link)->rb_left;
48 link = &(*link)->rb_right;
50 rb_link_node(&new->rbnode_phys, parent, link);
51 rb_insert_color(&new->rbnode_phys, &phys_to_mach);
56 pr_warn("%s: cannot add pfn=%pa -> mfn=%pa: pfn=%pa -> mfn=%pa already exists\n",
57 __func__, &new->pfn, &new->mfn, &entry->pfn, &entry->mfn);
62 unsigned long __pfn_to_mfn(unsigned long pfn)
65 struct xen_p2m_entry *entry;
66 unsigned long irqflags;
68 read_lock_irqsave(&p2m_lock, irqflags);
69 n = phys_to_mach.rb_node;
71 entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
72 if (entry->pfn <= pfn &&
73 entry->pfn + entry->nr_pages > pfn) {
74 read_unlock_irqrestore(&p2m_lock, irqflags);
75 return entry->mfn + (pfn - entry->pfn);
82 read_unlock_irqrestore(&p2m_lock, irqflags);
84 return INVALID_P2M_ENTRY;
86 EXPORT_SYMBOL_GPL(__pfn_to_mfn);
88 int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
89 struct gnttab_map_grant_ref *kmap_ops,
90 struct page **pages, unsigned int count)
94 for (i = 0; i < count; i++) {
95 struct gnttab_unmap_grant_ref unmap;
98 if (map_ops[i].status)
100 if (likely(set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT,
101 map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT)))
105 * Signal an error for this slot. This in turn requires
106 * immediate unmapping.
108 map_ops[i].status = GNTST_general_error;
109 unmap.host_addr = map_ops[i].host_addr,
110 unmap.handle = map_ops[i].handle;
111 map_ops[i].handle = ~0;
112 if (map_ops[i].flags & GNTMAP_device_map)
113 unmap.dev_bus_addr = map_ops[i].dev_bus_addr;
115 unmap.dev_bus_addr = 0;
118 * Pre-populate the status field, to be recognizable in
119 * the log message below.
123 rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
125 if (rc || unmap.status != GNTST_okay)
126 pr_err_once("gnttab unmap failed: rc=%d st=%d\n",
132 EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
134 int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
135 struct gnttab_unmap_grant_ref *kunmap_ops,
136 struct page **pages, unsigned int count)
140 for (i = 0; i < count; i++) {
141 set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
147 EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
149 bool __set_phys_to_machine_multi(unsigned long pfn,
150 unsigned long mfn, unsigned long nr_pages)
153 unsigned long irqflags;
154 struct xen_p2m_entry *p2m_entry;
157 if (mfn == INVALID_P2M_ENTRY) {
158 write_lock_irqsave(&p2m_lock, irqflags);
159 n = phys_to_mach.rb_node;
161 p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
162 if (p2m_entry->pfn <= pfn &&
163 p2m_entry->pfn + p2m_entry->nr_pages > pfn) {
164 rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach);
165 write_unlock_irqrestore(&p2m_lock, irqflags);
169 if (pfn < p2m_entry->pfn)
174 write_unlock_irqrestore(&p2m_lock, irqflags);
178 p2m_entry = kzalloc(sizeof(*p2m_entry), GFP_NOWAIT);
182 p2m_entry->pfn = pfn;
183 p2m_entry->nr_pages = nr_pages;
184 p2m_entry->mfn = mfn;
186 write_lock_irqsave(&p2m_lock, irqflags);
187 rc = xen_add_phys_to_mach_entry(p2m_entry);
189 write_unlock_irqrestore(&p2m_lock, irqflags);
192 write_unlock_irqrestore(&p2m_lock, irqflags);
195 EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
197 bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
199 return __set_phys_to_machine_multi(pfn, mfn, 1);
201 EXPORT_SYMBOL_GPL(__set_phys_to_machine);
203 static int p2m_init(void)
205 rwlock_init(&p2m_lock);
208 arch_initcall(p2m_init);