GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / arm / xen / p2m.c
1 #include <linux/bootmem.h>
2 #include <linux/gfp.h>
3 #include <linux/export.h>
4 #include <linux/spinlock.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
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
62 unsigned long __pfn_to_mfn(unsigned long pfn)
63 {
64         struct rb_node *n;
65         struct xen_p2m_entry *entry;
66         unsigned long irqflags;
67
68         read_lock_irqsave(&p2m_lock, irqflags);
69         n = phys_to_mach.rb_node;
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) {
74                         read_unlock_irqrestore(&p2m_lock, irqflags);
75                         return entry->mfn + (pfn - entry->pfn);
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 }
86 EXPORT_SYMBOL_GPL(__pfn_to_mfn);
87
88 int 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                 struct gnttab_unmap_grant_ref unmap;
96                 int rc;
97
98                 if (map_ops[i].status)
99                         continue;
100                 if (likely(set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT,
101                                     map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT)))
102                         continue;
103
104                 /*
105                  * Signal an error for this slot. This in turn requires
106                  * immediate unmapping.
107                  */
108                 map_ops[i].status = GNTST_general_error;
109                 unmap.host_addr = map_ops[i].host_addr,
110                 unmap.handle = map_ops[i].handle;
111                 map_ops[i].handle = ~0;
112                 if (map_ops[i].flags & GNTMAP_device_map)
113                         unmap.dev_bus_addr = map_ops[i].dev_bus_addr;
114                 else
115                         unmap.dev_bus_addr = 0;
116
117                 /*
118                  * Pre-populate the status field, to be recognizable in
119                  * the log message below.
120                  */
121                 unmap.status = 1;
122
123                 rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
124                                                &unmap, 1);
125                 if (rc || unmap.status != GNTST_okay)
126                         pr_err_once("gnttab unmap failed: rc=%d st=%d\n",
127                                     rc, unmap.status);
128         }
129
130         return 0;
131 }
132 EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
133
134 int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
135                               struct gnttab_unmap_grant_ref *kunmap_ops,
136                               struct page **pages, unsigned int count)
137 {
138         int i;
139
140         for (i = 0; i < count; i++) {
141                 set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
142                                     INVALID_P2M_ENTRY);
143         }
144
145         return 0;
146 }
147 EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
148
149 bool __set_phys_to_machine_multi(unsigned long pfn,
150                 unsigned long mfn, unsigned long nr_pages)
151 {
152         int rc;
153         unsigned long irqflags;
154         struct xen_p2m_entry *p2m_entry;
155         struct rb_node *n;
156
157         if (mfn == INVALID_P2M_ENTRY) {
158                 write_lock_irqsave(&p2m_lock, irqflags);
159                 n = phys_to_mach.rb_node;
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);
190                 return false;
191         }
192         write_unlock_irqrestore(&p2m_lock, irqflags);
193         return true;
194 }
195 EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
196
197 bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
198 {
199         return __set_phys_to_machine_multi(pfn, mfn, 1);
200 }
201 EXPORT_SYMBOL_GPL(__set_phys_to_machine);
202
203 static int p2m_init(void)
204 {
205         rwlock_init(&p2m_lock);
206         return 0;
207 }
208 arch_initcall(p2m_init);