David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | #include <linux/memblock.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3 | #include <linux/gfp.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> |
| 11 | |
| 12 | #include <xen/xen.h> |
| 13 | #include <xen/interface/memory.h> |
| 14 | #include <xen/page.h> |
| 15 | #include <xen/swiotlb-xen.h> |
| 16 | |
| 17 | #include <asm/cacheflush.h> |
| 18 | #include <asm/xen/hypercall.h> |
| 19 | #include <asm/xen/interface.h> |
| 20 | |
| 21 | struct xen_p2m_entry { |
| 22 | unsigned long pfn; |
| 23 | unsigned long mfn; |
| 24 | unsigned long nr_pages; |
| 25 | struct rb_node rbnode_phys; |
| 26 | }; |
| 27 | |
| 28 | static rwlock_t p2m_lock; |
| 29 | struct rb_root phys_to_mach = RB_ROOT; |
| 30 | EXPORT_SYMBOL_GPL(phys_to_mach); |
| 31 | |
| 32 | static int xen_add_phys_to_mach_entry(struct xen_p2m_entry *new) |
| 33 | { |
| 34 | struct rb_node **link = &phys_to_mach.rb_node; |
| 35 | struct rb_node *parent = NULL; |
| 36 | struct xen_p2m_entry *entry; |
| 37 | int rc = 0; |
| 38 | |
| 39 | while (*link) { |
| 40 | parent = *link; |
| 41 | entry = rb_entry(parent, struct xen_p2m_entry, rbnode_phys); |
| 42 | |
| 43 | if (new->pfn == entry->pfn) |
| 44 | goto err_out; |
| 45 | |
| 46 | if (new->pfn < entry->pfn) |
| 47 | link = &(*link)->rb_left; |
| 48 | else |
| 49 | link = &(*link)->rb_right; |
| 50 | } |
| 51 | rb_link_node(&new->rbnode_phys, parent, link); |
| 52 | rb_insert_color(&new->rbnode_phys, &phys_to_mach); |
| 53 | goto out; |
| 54 | |
| 55 | err_out: |
| 56 | rc = -EINVAL; |
| 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); |
| 59 | out: |
| 60 | return rc; |
| 61 | } |
| 62 | |
| 63 | unsigned long __pfn_to_mfn(unsigned long pfn) |
| 64 | { |
| 65 | struct rb_node *n = phys_to_mach.rb_node; |
| 66 | struct xen_p2m_entry *entry; |
| 67 | unsigned long irqflags; |
| 68 | |
| 69 | read_lock_irqsave(&p2m_lock, irqflags); |
| 70 | while (n) { |
| 71 | entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys); |
| 72 | if (entry->pfn <= pfn && |
| 73 | entry->pfn + entry->nr_pages > pfn) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 74 | unsigned long mfn = entry->mfn + (pfn - entry->pfn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | read_unlock_irqrestore(&p2m_lock, irqflags); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 76 | return mfn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 77 | } |
| 78 | if (pfn < entry->pfn) |
| 79 | n = n->rb_left; |
| 80 | else |
| 81 | n = n->rb_right; |
| 82 | } |
| 83 | read_unlock_irqrestore(&p2m_lock, irqflags); |
| 84 | |
| 85 | return INVALID_P2M_ENTRY; |
| 86 | } |
| 87 | EXPORT_SYMBOL_GPL(__pfn_to_mfn); |
| 88 | |
| 89 | int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops, |
| 90 | struct gnttab_map_grant_ref *kmap_ops, |
| 91 | struct page **pages, unsigned int count) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | for (i = 0; i < count; i++) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 96 | struct gnttab_unmap_grant_ref unmap; |
| 97 | int rc; |
| 98 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 99 | if (map_ops[i].status) |
| 100 | continue; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 101 | if (likely(set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT, |
| 102 | map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT))) |
| 103 | continue; |
| 104 | |
| 105 | /* |
| 106 | * Signal an error for this slot. This in turn requires |
| 107 | * immediate unmapping. |
| 108 | */ |
| 109 | map_ops[i].status = GNTST_general_error; |
| 110 | unmap.host_addr = map_ops[i].host_addr, |
| 111 | unmap.handle = map_ops[i].handle; |
| 112 | map_ops[i].handle = ~0; |
| 113 | if (map_ops[i].flags & GNTMAP_device_map) |
| 114 | unmap.dev_bus_addr = map_ops[i].dev_bus_addr; |
| 115 | else |
| 116 | unmap.dev_bus_addr = 0; |
| 117 | |
| 118 | /* |
| 119 | * Pre-populate the status field, to be recognizable in |
| 120 | * the log message below. |
| 121 | */ |
| 122 | unmap.status = 1; |
| 123 | |
| 124 | rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, |
| 125 | &unmap, 1); |
| 126 | if (rc || unmap.status != GNTST_okay) |
| 127 | pr_err_once("gnttab unmap failed: rc=%d st=%d\n", |
| 128 | rc, unmap.status); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping); |
| 134 | |
| 135 | int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops, |
| 136 | struct gnttab_unmap_grant_ref *kunmap_ops, |
| 137 | struct page **pages, unsigned int count) |
| 138 | { |
| 139 | int i; |
| 140 | |
| 141 | for (i = 0; i < count; i++) { |
| 142 | set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT, |
| 143 | INVALID_P2M_ENTRY); |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping); |
| 149 | |
| 150 | bool __set_phys_to_machine_multi(unsigned long pfn, |
| 151 | unsigned long mfn, unsigned long nr_pages) |
| 152 | { |
| 153 | int rc; |
| 154 | unsigned long irqflags; |
| 155 | struct xen_p2m_entry *p2m_entry; |
| 156 | struct rb_node *n = phys_to_mach.rb_node; |
| 157 | |
| 158 | if (mfn == INVALID_P2M_ENTRY) { |
| 159 | write_lock_irqsave(&p2m_lock, irqflags); |
| 160 | while (n) { |
| 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); |
| 166 | kfree(p2m_entry); |
| 167 | return true; |
| 168 | } |
| 169 | if (pfn < p2m_entry->pfn) |
| 170 | n = n->rb_left; |
| 171 | else |
| 172 | n = n->rb_right; |
| 173 | } |
| 174 | write_unlock_irqrestore(&p2m_lock, irqflags); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | p2m_entry = kzalloc(sizeof(*p2m_entry), GFP_NOWAIT); |
| 179 | if (!p2m_entry) |
| 180 | return false; |
| 181 | |
| 182 | p2m_entry->pfn = pfn; |
| 183 | p2m_entry->nr_pages = nr_pages; |
| 184 | p2m_entry->mfn = mfn; |
| 185 | |
| 186 | write_lock_irqsave(&p2m_lock, irqflags); |
| 187 | rc = xen_add_phys_to_mach_entry(p2m_entry); |
| 188 | if (rc < 0) { |
| 189 | write_unlock_irqrestore(&p2m_lock, irqflags); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 190 | kfree(p2m_entry); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | write_unlock_irqrestore(&p2m_lock, irqflags); |
| 194 | return true; |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi); |
| 197 | |
| 198 | bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn) |
| 199 | { |
| 200 | return __set_phys_to_machine_multi(pfn, mfn, 1); |
| 201 | } |
| 202 | EXPORT_SYMBOL_GPL(__set_phys_to_machine); |
| 203 | |
| 204 | static int p2m_init(void) |
| 205 | { |
| 206 | rwlock_init(&p2m_lock); |
| 207 | return 0; |
| 208 | } |
| 209 | arch_initcall(p2m_init); |