Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/memblock.h>
0003 #include <linux/gfp.h>
0004 #include <linux/export.h>
0005 #include <linux/spinlock.h>
0006 #include <linux/slab.h>
0007 #include <linux/types.h>
0008 #include <linux/dma-mapping.h>
0009 #include <linux/vmalloc.h>
0010 #include <linux/swiotlb.h>
0011 
0012 #include <xen/xen.h>
0013 #include <xen/interface/memory.h>
0014 #include <xen/grant_table.h>
0015 #include <xen/page.h>
0016 #include <xen/swiotlb-xen.h>
0017 
0018 #include <asm/cacheflush.h>
0019 #include <asm/xen/hypercall.h>
0020 #include <asm/xen/interface.h>
0021 
0022 struct xen_p2m_entry {
0023     unsigned long pfn;
0024     unsigned long mfn;
0025     unsigned long nr_pages;
0026     struct rb_node rbnode_phys;
0027 };
0028 
0029 static rwlock_t p2m_lock;
0030 struct rb_root phys_to_mach = RB_ROOT;
0031 EXPORT_SYMBOL_GPL(phys_to_mach);
0032 
0033 static int xen_add_phys_to_mach_entry(struct xen_p2m_entry *new)
0034 {
0035     struct rb_node **link = &phys_to_mach.rb_node;
0036     struct rb_node *parent = NULL;
0037     struct xen_p2m_entry *entry;
0038     int rc = 0;
0039 
0040     while (*link) {
0041         parent = *link;
0042         entry = rb_entry(parent, struct xen_p2m_entry, rbnode_phys);
0043 
0044         if (new->pfn == entry->pfn)
0045             goto err_out;
0046 
0047         if (new->pfn < entry->pfn)
0048             link = &(*link)->rb_left;
0049         else
0050             link = &(*link)->rb_right;
0051     }
0052     rb_link_node(&new->rbnode_phys, parent, link);
0053     rb_insert_color(&new->rbnode_phys, &phys_to_mach);
0054     goto out;
0055 
0056 err_out:
0057     rc = -EINVAL;
0058     pr_warn("%s: cannot add pfn=%pa -> mfn=%pa: pfn=%pa -> mfn=%pa already exists\n",
0059             __func__, &new->pfn, &new->mfn, &entry->pfn, &entry->mfn);
0060 out:
0061     return rc;
0062 }
0063 
0064 unsigned long __pfn_to_mfn(unsigned long pfn)
0065 {
0066     struct rb_node *n;
0067     struct xen_p2m_entry *entry;
0068     unsigned long irqflags;
0069 
0070     read_lock_irqsave(&p2m_lock, irqflags);
0071     n = phys_to_mach.rb_node;
0072     while (n) {
0073         entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
0074         if (entry->pfn <= pfn &&
0075                 entry->pfn + entry->nr_pages > pfn) {
0076             unsigned long mfn = entry->mfn + (pfn - entry->pfn);
0077             read_unlock_irqrestore(&p2m_lock, irqflags);
0078             return mfn;
0079         }
0080         if (pfn < entry->pfn)
0081             n = n->rb_left;
0082         else
0083             n = n->rb_right;
0084     }
0085     read_unlock_irqrestore(&p2m_lock, irqflags);
0086 
0087     return INVALID_P2M_ENTRY;
0088 }
0089 EXPORT_SYMBOL_GPL(__pfn_to_mfn);
0090 
0091 int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
0092                 struct gnttab_map_grant_ref *kmap_ops,
0093                 struct page **pages, unsigned int count)
0094 {
0095     int i;
0096 
0097     for (i = 0; i < count; i++) {
0098         struct gnttab_unmap_grant_ref unmap;
0099         int rc;
0100 
0101         if (map_ops[i].status)
0102             continue;
0103         if (likely(set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT,
0104                     map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT)))
0105             continue;
0106 
0107         /*
0108          * Signal an error for this slot. This in turn requires
0109          * immediate unmapping.
0110          */
0111         map_ops[i].status = GNTST_general_error;
0112         unmap.host_addr = map_ops[i].host_addr,
0113         unmap.handle = map_ops[i].handle;
0114         map_ops[i].handle = INVALID_GRANT_HANDLE;
0115         if (map_ops[i].flags & GNTMAP_device_map)
0116             unmap.dev_bus_addr = map_ops[i].dev_bus_addr;
0117         else
0118             unmap.dev_bus_addr = 0;
0119 
0120         /*
0121          * Pre-populate the status field, to be recognizable in
0122          * the log message below.
0123          */
0124         unmap.status = 1;
0125 
0126         rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
0127                            &unmap, 1);
0128         if (rc || unmap.status != GNTST_okay)
0129             pr_err_once("gnttab unmap failed: rc=%d st=%d\n",
0130                     rc, unmap.status);
0131     }
0132 
0133     return 0;
0134 }
0135 
0136 int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
0137                   struct gnttab_unmap_grant_ref *kunmap_ops,
0138                   struct page **pages, unsigned int count)
0139 {
0140     int i;
0141 
0142     for (i = 0; i < count; i++) {
0143         set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
0144                     INVALID_P2M_ENTRY);
0145     }
0146 
0147     return 0;
0148 }
0149 
0150 bool __set_phys_to_machine_multi(unsigned long pfn,
0151         unsigned long mfn, unsigned long nr_pages)
0152 {
0153     int rc;
0154     unsigned long irqflags;
0155     struct xen_p2m_entry *p2m_entry;
0156     struct rb_node *n;
0157 
0158     if (mfn == INVALID_P2M_ENTRY) {
0159         write_lock_irqsave(&p2m_lock, irqflags);
0160         n = phys_to_mach.rb_node;
0161         while (n) {
0162             p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
0163             if (p2m_entry->pfn <= pfn &&
0164                     p2m_entry->pfn + p2m_entry->nr_pages > pfn) {
0165                 rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach);
0166                 write_unlock_irqrestore(&p2m_lock, irqflags);
0167                 kfree(p2m_entry);
0168                 return true;
0169             }
0170             if (pfn < p2m_entry->pfn)
0171                 n = n->rb_left;
0172             else
0173                 n = n->rb_right;
0174         }
0175         write_unlock_irqrestore(&p2m_lock, irqflags);
0176         return true;
0177     }
0178 
0179     p2m_entry = kzalloc(sizeof(*p2m_entry), GFP_NOWAIT);
0180     if (!p2m_entry)
0181         return false;
0182 
0183     p2m_entry->pfn = pfn;
0184     p2m_entry->nr_pages = nr_pages;
0185     p2m_entry->mfn = mfn;
0186 
0187     write_lock_irqsave(&p2m_lock, irqflags);
0188     rc = xen_add_phys_to_mach_entry(p2m_entry);
0189     if (rc < 0) {
0190         write_unlock_irqrestore(&p2m_lock, irqflags);
0191         kfree(p2m_entry);
0192         return false;
0193     }
0194     write_unlock_irqrestore(&p2m_lock, irqflags);
0195     return true;
0196 }
0197 EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
0198 
0199 bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
0200 {
0201     return __set_phys_to_machine_multi(pfn, mfn, 1);
0202 }
0203 EXPORT_SYMBOL_GPL(__set_phys_to_machine);
0204 
0205 static int p2m_init(void)
0206 {
0207     rwlock_init(&p2m_lock);
0208     return 0;
0209 }
0210 arch_initcall(p2m_init);