1 #include <linux/bootmem.h>
3 #include <linux/export.h>
4 #include <linux/rwlock.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)
64 struct rb_node *n = phys_to_mach.rb_node;
65 struct xen_p2m_entry *entry;
66 unsigned long irqflags;
68 read_lock_irqsave(&p2m_lock, irqflags);
70 entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
71 if (entry->pfn <= pfn &&
72 entry->pfn + entry->nr_pages > pfn) {
73 read_unlock_irqrestore(&p2m_lock, irqflags);
74 return entry->mfn + (pfn - entry->pfn);
81 read_unlock_irqrestore(&p2m_lock, irqflags);
83 return INVALID_P2M_ENTRY;
85 EXPORT_SYMBOL_GPL(__pfn_to_mfn);
87 int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
88 struct gnttab_map_grant_ref *kmap_ops,
89 struct page **pages, unsigned int count)
93 for (i = 0; i < count; i++) {
94 struct gnttab_unmap_grant_ref unmap;
97 if (map_ops[i].status)
99 if (likely(set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT,
100 map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT)))
104 * Signal an error for this slot. This in turn requires
105 * immediate unmapping.
107 map_ops[i].status = GNTST_general_error;
108 unmap.host_addr = map_ops[i].host_addr,
109 unmap.handle = map_ops[i].handle;
110 map_ops[i].handle = ~0;
111 if (map_ops[i].flags & GNTMAP_device_map)
112 unmap.dev_bus_addr = map_ops[i].dev_bus_addr;
114 unmap.dev_bus_addr = 0;
117 * Pre-populate the status field, to be recognizable in
118 * the log message below.
122 rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
124 if (rc || unmap.status != GNTST_okay)
125 pr_err_once("gnttab unmap failed: rc=%d st=%d\n",
131 EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
133 int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
134 struct gnttab_unmap_grant_ref *kunmap_ops,
135 struct page **pages, unsigned int count)
139 for (i = 0; i < count; i++) {
140 set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
146 EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
148 bool __set_phys_to_machine_multi(unsigned long pfn,
149 unsigned long mfn, unsigned long nr_pages)
152 unsigned long irqflags;
153 struct xen_p2m_entry *p2m_entry;
154 struct rb_node *n = phys_to_mach.rb_node;
156 if (mfn == INVALID_P2M_ENTRY) {
157 write_lock_irqsave(&p2m_lock, irqflags);
159 p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
160 if (p2m_entry->pfn <= pfn &&
161 p2m_entry->pfn + p2m_entry->nr_pages > pfn) {
162 rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach);
163 write_unlock_irqrestore(&p2m_lock, irqflags);
167 if (pfn < p2m_entry->pfn)
172 write_unlock_irqrestore(&p2m_lock, irqflags);
176 p2m_entry = kzalloc(sizeof(struct xen_p2m_entry), GFP_NOWAIT);
178 pr_warn("cannot allocate xen_p2m_entry\n");
181 p2m_entry->pfn = pfn;
182 p2m_entry->nr_pages = nr_pages;
183 p2m_entry->mfn = mfn;
185 write_lock_irqsave(&p2m_lock, irqflags);
186 if ((rc = xen_add_phys_to_mach_entry(p2m_entry)) < 0) {
187 write_unlock_irqrestore(&p2m_lock, irqflags);
190 write_unlock_irqrestore(&p2m_lock, irqflags);
193 EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
195 bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
197 return __set_phys_to_machine_multi(pfn, mfn, 1);
199 EXPORT_SYMBOL_GPL(__set_phys_to_machine);
201 static int p2m_init(void)
203 rwlock_init(&p2m_lock);
206 arch_initcall(p2m_init);