]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/arm/xen/p2m.c
Merge tag 'powerpc-5.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[mirror_ubuntu-hirsute-kernel.git] / arch / arm / xen / p2m.c
CommitLineData
57c8a661 1#include <linux/memblock.h>
4a19138c
SS
2#include <linux/gfp.h>
3#include <linux/export.h>
a494ee6c 4#include <linux/spinlock.h>
4a19138c
SS
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>
a9fd60e2 13#include <xen/page.h>
4a19138c
SS
14#include <xen/swiotlb-xen.h>
15
16#include <asm/cacheflush.h>
4a19138c
SS
17#include <asm/xen/hypercall.h>
18#include <asm/xen/interface.h>
19
20struct xen_p2m_entry {
21 unsigned long pfn;
22 unsigned long mfn;
23 unsigned long nr_pages;
4a19138c
SS
24 struct rb_node rbnode_phys;
25};
26
f9c7ec16 27static rwlock_t p2m_lock;
4a19138c 28struct rb_root phys_to_mach = RB_ROOT;
c8999a88 29EXPORT_SYMBOL_GPL(phys_to_mach);
4a19138c
SS
30
31static 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
4a19138c
SS
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
54err_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);
58out:
59 return rc;
60}
61
62unsigned 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) {
fe846979 73 unsigned long mfn = entry->mfn + (pfn - entry->pfn);
4a19138c 74 read_unlock_irqrestore(&p2m_lock, irqflags);
fe846979 75 return mfn;
4a19138c
SS
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}
86EXPORT_SYMBOL_GPL(__pfn_to_mfn);
87
1429d46d
ZK
88int 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;
5ed5451d
JG
97 set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT,
98 map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT);
1429d46d
ZK
99 }
100
101 return 0;
102}
103EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
104
105int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
853d0289 106 struct gnttab_unmap_grant_ref *kunmap_ops,
1429d46d
ZK
107 struct page **pages, unsigned int count)
108{
109 int i;
110
111 for (i = 0; i < count; i++) {
5ed5451d 112 set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
1429d46d
ZK
113 INVALID_P2M_ENTRY);
114 }
115
116 return 0;
117}
118EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
119
4a19138c
SS
120bool __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) {
4a19138c
SS
134 rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach);
135 write_unlock_irqrestore(&p2m_lock, irqflags);
e1d8f62a 136 kfree(p2m_entry);
4a19138c
SS
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
031229b8 148 p2m_entry = kzalloc(sizeof(*p2m_entry), GFP_NOWAIT);
d6bb4ec3 149 if (!p2m_entry)
4a19138c 150 return false;
d6bb4ec3 151
4a19138c
SS
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);
84a0a967
ME
157 rc = xen_add_phys_to_mach_entry(p2m_entry);
158 if (rc < 0) {
4a19138c 159 write_unlock_irqrestore(&p2m_lock, irqflags);
425f1cc2 160 kfree(p2m_entry);
4a19138c
SS
161 return false;
162 }
163 write_unlock_irqrestore(&p2m_lock, irqflags);
164 return true;
165}
166EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
167
168bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
169{
170 return __set_phys_to_machine_multi(pfn, mfn, 1);
171}
172EXPORT_SYMBOL_GPL(__set_phys_to_machine);
173
f9c7ec16 174static int p2m_init(void)
4a19138c
SS
175{
176 rwlock_init(&p2m_lock);
177 return 0;
178}
179arch_initcall(p2m_init);