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>
13 #include <xen/swiotlb-xen.h>
15 #include <asm/cacheflush.h>
16 #include <asm/xen/page.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_mach
;
25 struct rb_node rbnode_phys
;
28 static rwlock_t p2m_lock
;
29 struct rb_root phys_to_mach
= RB_ROOT
;
30 EXPORT_SYMBOL_GPL(phys_to_mach
);
31 static struct rb_root mach_to_phys
= RB_ROOT
;
33 static int xen_add_phys_to_mach_entry(struct xen_p2m_entry
*new)
35 struct rb_node
**link
= &phys_to_mach
.rb_node
;
36 struct rb_node
*parent
= NULL
;
37 struct xen_p2m_entry
*entry
;
42 entry
= rb_entry(parent
, struct xen_p2m_entry
, rbnode_phys
);
44 if (new->mfn
== entry
->mfn
)
46 if (new->pfn
== entry
->pfn
)
49 if (new->pfn
< entry
->pfn
)
50 link
= &(*link
)->rb_left
;
52 link
= &(*link
)->rb_right
;
54 rb_link_node(&new->rbnode_phys
, parent
, link
);
55 rb_insert_color(&new->rbnode_phys
, &phys_to_mach
);
60 pr_warn("%s: cannot add pfn=%pa -> mfn=%pa: pfn=%pa -> mfn=%pa already exists\n",
61 __func__
, &new->pfn
, &new->mfn
, &entry
->pfn
, &entry
->mfn
);
66 unsigned long __pfn_to_mfn(unsigned long pfn
)
68 struct rb_node
*n
= phys_to_mach
.rb_node
;
69 struct xen_p2m_entry
*entry
;
70 unsigned long irqflags
;
72 read_lock_irqsave(&p2m_lock
, irqflags
);
74 entry
= rb_entry(n
, struct xen_p2m_entry
, rbnode_phys
);
75 if (entry
->pfn
<= pfn
&&
76 entry
->pfn
+ entry
->nr_pages
> pfn
) {
77 read_unlock_irqrestore(&p2m_lock
, irqflags
);
78 return entry
->mfn
+ (pfn
- entry
->pfn
);
85 read_unlock_irqrestore(&p2m_lock
, irqflags
);
87 return INVALID_P2M_ENTRY
;
89 EXPORT_SYMBOL_GPL(__pfn_to_mfn
);
91 static int xen_add_mach_to_phys_entry(struct xen_p2m_entry
*new)
93 struct rb_node
**link
= &mach_to_phys
.rb_node
;
94 struct rb_node
*parent
= NULL
;
95 struct xen_p2m_entry
*entry
;
100 entry
= rb_entry(parent
, struct xen_p2m_entry
, rbnode_mach
);
102 if (new->mfn
== entry
->mfn
)
104 if (new->pfn
== entry
->pfn
)
107 if (new->mfn
< entry
->mfn
)
108 link
= &(*link
)->rb_left
;
110 link
= &(*link
)->rb_right
;
112 rb_link_node(&new->rbnode_mach
, parent
, link
);
113 rb_insert_color(&new->rbnode_mach
, &mach_to_phys
);
118 pr_warn("%s: cannot add pfn=%pa -> mfn=%pa: pfn=%pa -> mfn=%pa already exists\n",
119 __func__
, &new->pfn
, &new->mfn
, &entry
->pfn
, &entry
->mfn
);
124 unsigned long __mfn_to_pfn(unsigned long mfn
)
126 struct rb_node
*n
= mach_to_phys
.rb_node
;
127 struct xen_p2m_entry
*entry
;
128 unsigned long irqflags
;
130 read_lock_irqsave(&p2m_lock
, irqflags
);
132 entry
= rb_entry(n
, struct xen_p2m_entry
, rbnode_mach
);
133 if (entry
->mfn
<= mfn
&&
134 entry
->mfn
+ entry
->nr_pages
> mfn
) {
135 read_unlock_irqrestore(&p2m_lock
, irqflags
);
136 return entry
->pfn
+ (mfn
- entry
->mfn
);
138 if (mfn
< entry
->mfn
)
143 read_unlock_irqrestore(&p2m_lock
, irqflags
);
145 return INVALID_P2M_ENTRY
;
147 EXPORT_SYMBOL_GPL(__mfn_to_pfn
);
149 int set_foreign_p2m_mapping(struct gnttab_map_grant_ref
*map_ops
,
150 struct gnttab_map_grant_ref
*kmap_ops
,
151 struct page
**pages
, unsigned int count
)
155 for (i
= 0; i
< count
; i
++) {
156 if (map_ops
[i
].status
)
158 set_phys_to_machine(map_ops
[i
].host_addr
>> PAGE_SHIFT
,
159 map_ops
[i
].dev_bus_addr
>> PAGE_SHIFT
);
164 EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping
);
166 int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref
*unmap_ops
,
167 struct gnttab_map_grant_ref
*kmap_ops
,
168 struct page
**pages
, unsigned int count
)
172 for (i
= 0; i
< count
; i
++) {
173 set_phys_to_machine(unmap_ops
[i
].host_addr
>> PAGE_SHIFT
,
179 EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping
);
181 bool __set_phys_to_machine_multi(unsigned long pfn
,
182 unsigned long mfn
, unsigned long nr_pages
)
185 unsigned long irqflags
;
186 struct xen_p2m_entry
*p2m_entry
;
187 struct rb_node
*n
= phys_to_mach
.rb_node
;
189 if (mfn
== INVALID_P2M_ENTRY
) {
190 write_lock_irqsave(&p2m_lock
, irqflags
);
192 p2m_entry
= rb_entry(n
, struct xen_p2m_entry
, rbnode_phys
);
193 if (p2m_entry
->pfn
<= pfn
&&
194 p2m_entry
->pfn
+ p2m_entry
->nr_pages
> pfn
) {
195 rb_erase(&p2m_entry
->rbnode_mach
, &mach_to_phys
);
196 rb_erase(&p2m_entry
->rbnode_phys
, &phys_to_mach
);
197 write_unlock_irqrestore(&p2m_lock
, irqflags
);
201 if (pfn
< p2m_entry
->pfn
)
206 write_unlock_irqrestore(&p2m_lock
, irqflags
);
210 p2m_entry
= kzalloc(sizeof(struct xen_p2m_entry
), GFP_NOWAIT
);
212 pr_warn("cannot allocate xen_p2m_entry\n");
215 p2m_entry
->pfn
= pfn
;
216 p2m_entry
->nr_pages
= nr_pages
;
217 p2m_entry
->mfn
= mfn
;
219 write_lock_irqsave(&p2m_lock
, irqflags
);
220 if ((rc
= xen_add_phys_to_mach_entry(p2m_entry
) < 0) ||
221 (rc
= xen_add_mach_to_phys_entry(p2m_entry
) < 0)) {
222 write_unlock_irqrestore(&p2m_lock
, irqflags
);
225 write_unlock_irqrestore(&p2m_lock
, irqflags
);
228 EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi
);
230 bool __set_phys_to_machine(unsigned long pfn
, unsigned long mfn
)
232 return __set_phys_to_machine_multi(pfn
, mfn
, 1);
234 EXPORT_SYMBOL_GPL(__set_phys_to_machine
);
236 static int p2m_init(void)
238 rwlock_init(&p2m_lock
);
241 arch_initcall(p2m_init
);