• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/bootmem.h>
2 #include <linux/gfp.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>
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 
xen_add_phys_to_mach_entry(struct xen_p2m_entry * new)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 
__pfn_to_mfn(unsigned long pfn)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 			read_unlock_irqrestore(&p2m_lock, irqflags);
74 			return entry->mfn + (pfn - entry->pfn);
75 		}
76 		if (pfn < entry->pfn)
77 			n = n->rb_left;
78 		else
79 			n = n->rb_right;
80 	}
81 	read_unlock_irqrestore(&p2m_lock, irqflags);
82 
83 	return INVALID_P2M_ENTRY;
84 }
85 EXPORT_SYMBOL_GPL(__pfn_to_mfn);
86 
set_foreign_p2m_mapping(struct gnttab_map_grant_ref * map_ops,struct gnttab_map_grant_ref * kmap_ops,struct page ** pages,unsigned int count)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)
90 {
91 	int i;
92 
93 	for (i = 0; i < count; i++) {
94 		struct gnttab_unmap_grant_ref unmap;
95 		int rc;
96 
97 		if (map_ops[i].status)
98 			continue;
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)))
101 			continue;
102 
103 		/*
104 		 * Signal an error for this slot. This in turn requires
105 		 * immediate unmapping.
106 		 */
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;
113 		else
114 			unmap.dev_bus_addr = 0;
115 
116 		/*
117 		 * Pre-populate the status field, to be recognizable in
118 		 * the log message below.
119 		 */
120 		unmap.status = 1;
121 
122 		rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
123 					       &unmap, 1);
124 		if (rc || unmap.status != GNTST_okay)
125 			pr_err_once("gnttab unmap failed: rc=%d st=%d\n",
126 				    rc, unmap.status);
127 	}
128 
129 	return 0;
130 }
131 EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
132 
clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref * unmap_ops,struct gnttab_unmap_grant_ref * kunmap_ops,struct page ** pages,unsigned int count)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)
136 {
137 	int i;
138 
139 	for (i = 0; i < count; i++) {
140 		set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
141 				    INVALID_P2M_ENTRY);
142 	}
143 
144 	return 0;
145 }
146 EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
147 
__set_phys_to_machine_multi(unsigned long pfn,unsigned long mfn,unsigned long nr_pages)148 bool __set_phys_to_machine_multi(unsigned long pfn,
149 		unsigned long mfn, unsigned long nr_pages)
150 {
151 	int rc;
152 	unsigned long irqflags;
153 	struct xen_p2m_entry *p2m_entry;
154 	struct rb_node *n = phys_to_mach.rb_node;
155 
156 	if (mfn == INVALID_P2M_ENTRY) {
157 		write_lock_irqsave(&p2m_lock, irqflags);
158 		while (n) {
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);
164 				kfree(p2m_entry);
165 				return true;
166 			}
167 			if (pfn < p2m_entry->pfn)
168 				n = n->rb_left;
169 			else
170 				n = n->rb_right;
171 		}
172 		write_unlock_irqrestore(&p2m_lock, irqflags);
173 		return true;
174 	}
175 
176 	p2m_entry = kzalloc(sizeof(struct xen_p2m_entry), GFP_NOWAIT);
177 	if (!p2m_entry) {
178 		pr_warn("cannot allocate xen_p2m_entry\n");
179 		return false;
180 	}
181 	p2m_entry->pfn = pfn;
182 	p2m_entry->nr_pages = nr_pages;
183 	p2m_entry->mfn = mfn;
184 
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);
188 		return false;
189 	}
190 	write_unlock_irqrestore(&p2m_lock, irqflags);
191 	return true;
192 }
193 EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
194 
__set_phys_to_machine(unsigned long pfn,unsigned long mfn)195 bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
196 {
197 	return __set_phys_to_machine_multi(pfn, mfn, 1);
198 }
199 EXPORT_SYMBOL_GPL(__set_phys_to_machine);
200 
p2m_init(void)201 static int p2m_init(void)
202 {
203 	rwlock_init(&p2m_lock);
204 	return 0;
205 }
206 arch_initcall(p2m_init);
207