1 #include <linux/memblock.h> 2 #include <linux/gfp.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> 10 11 #include <xen/xen.h> 12 #include <xen/interface/memory.h> 13 #include <xen/page.h> 14 #include <xen/swiotlb-xen.h> 15 16 #include <asm/cacheflush.h> 17 #include <asm/xen/hypercall.h> 18 #include <asm/xen/interface.h> 19 20 struct xen_p2m_entry { 21 unsigned long pfn; 22 unsigned long mfn; 23 unsigned long nr_pages; 24 struct rb_node rbnode_phys; 25 }; 26 27 static rwlock_t p2m_lock; 28 struct rb_root phys_to_mach = RB_ROOT; 29 EXPORT_SYMBOL_GPL(phys_to_mach); 30 31 static int xen_add_phys_to_mach_entry(struct xen_p2m_entry *new) 32 { 33 struct rb_node **link = &phys_to_mach.rb_node; 34 struct rb_node *parent = NULL; 35 struct xen_p2m_entry *entry; 36 int rc = 0; 37 38 while (*link) { 39 parent = *link; 40 entry = rb_entry(parent, struct xen_p2m_entry, rbnode_phys); 41 42 if (new->pfn == entry->pfn) 43 goto err_out; 44 45 if (new->pfn < entry->pfn) 46 link = &(*link)->rb_left; 47 else 48 link = &(*link)->rb_right; 49 } 50 rb_link_node(&new->rbnode_phys, parent, link); 51 rb_insert_color(&new->rbnode_phys, &phys_to_mach); 52 goto out; 53 54 err_out: 55 rc = -EINVAL; 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); 58 out: 59 return rc; 60 } 61 62 unsigned long __pfn_to_mfn(unsigned long pfn) 63 { 64 struct rb_node *n = phys_to_mach.rb_node; 65 struct xen_p2m_entry *entry; 66 unsigned long irqflags; 67 68 read_lock_irqsave(&p2m_lock, irqflags); 69 while (n) { 70 entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys); 71 if (entry->pfn <= pfn && 72 entry->pfn + entry->nr_pages > pfn) { 73 unsigned long mfn = entry->mfn + (pfn - entry->pfn); 74 read_unlock_irqrestore(&p2m_lock, irqflags); 75 return mfn; 76 } 77 if (pfn < entry->pfn) 78 n = n->rb_left; 79 else 80 n = n->rb_right; 81 } 82 read_unlock_irqrestore(&p2m_lock, irqflags); 83 84 return INVALID_P2M_ENTRY; 85 } 86 EXPORT_SYMBOL_GPL(__pfn_to_mfn); 87 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) 91 { 92 int i; 93 94 for (i = 0; i < count; i++) { 95 if (map_ops[i].status) 96 continue; 97 set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT, 98 map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT); 99 } 100 101 return 0; 102 } 103 EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping); 104 105 int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops, 106 struct gnttab_unmap_grant_ref *kunmap_ops, 107 struct page **pages, unsigned int count) 108 { 109 int i; 110 111 for (i = 0; i < count; i++) { 112 set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT, 113 INVALID_P2M_ENTRY); 114 } 115 116 return 0; 117 } 118 EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping); 119 120 bool __set_phys_to_machine_multi(unsigned long pfn, 121 unsigned long mfn, unsigned long nr_pages) 122 { 123 int rc; 124 unsigned long irqflags; 125 struct xen_p2m_entry *p2m_entry; 126 struct rb_node *n = phys_to_mach.rb_node; 127 128 if (mfn == INVALID_P2M_ENTRY) { 129 write_lock_irqsave(&p2m_lock, irqflags); 130 while (n) { 131 p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys); 132 if (p2m_entry->pfn <= pfn && 133 p2m_entry->pfn + p2m_entry->nr_pages > pfn) { 134 rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach); 135 write_unlock_irqrestore(&p2m_lock, irqflags); 136 kfree(p2m_entry); 137 return true; 138 } 139 if (pfn < p2m_entry->pfn) 140 n = n->rb_left; 141 else 142 n = n->rb_right; 143 } 144 write_unlock_irqrestore(&p2m_lock, irqflags); 145 return true; 146 } 147 148 p2m_entry = kzalloc(sizeof(*p2m_entry), GFP_NOWAIT); 149 if (!p2m_entry) 150 return false; 151 152 p2m_entry->pfn = pfn; 153 p2m_entry->nr_pages = nr_pages; 154 p2m_entry->mfn = mfn; 155 156 write_lock_irqsave(&p2m_lock, irqflags); 157 rc = xen_add_phys_to_mach_entry(p2m_entry); 158 if (rc < 0) { 159 write_unlock_irqrestore(&p2m_lock, irqflags); 160 kfree(p2m_entry); 161 return false; 162 } 163 write_unlock_irqrestore(&p2m_lock, irqflags); 164 return true; 165 } 166 EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi); 167 168 bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn) 169 { 170 return __set_phys_to_machine_multi(pfn, mfn, 1); 171 } 172 EXPORT_SYMBOL_GPL(__set_phys_to_machine); 173 174 static int p2m_init(void) 175 { 176 rwlock_init(&p2m_lock); 177 return 0; 178 } 179 arch_initcall(p2m_init); 180