FreeBSD/Linux Kernel Cross Reference
sys/dev/xen/bus/xenpv.c
1 /*
2 * Copyright (c) 2014 Roger Pau Monné <roger.pau@citrix.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/pcpu.h>
36 #include <sys/rman.h>
37 #include <sys/smp.h>
38 #include <sys/limits.h>
39 #include <sys/vmmeter.h>
40
41 #include <vm/vm.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_param.h>
44 #include <vm/vm_phys.h>
45
46 #include <xen/xen-os.h>
47 #include <xen/gnttab.h>
48
49 #include "xenmem_if.h"
50
51 /*
52 * Allocate unused physical memory above 4GB in order to map memory
53 * from foreign domains. We use memory starting at 4GB in order to
54 * prevent clashes with MMIO/ACPI regions.
55 *
56 * Since this is not possible on i386 just use any available memory
57 * chunk above 1MB and hope we don't clash with anything else.
58 *
59 * Other architectures better document MMIO regions and drivers more
60 * reliably reserve them. As such, allow using any unpopulated memory
61 * region.
62 */
63 #ifdef __amd64__
64 #define LOW_MEM_LIMIT 0x100000000ul
65 #elif defined(__i386__)
66 #define LOW_MEM_LIMIT 0x100000ul
67 #else
68 #define LOW_MEM_LIMIT 0
69 #endif
70
71 static void
72 xenpv_identify(driver_t *driver, device_t parent)
73 {
74 if (!xen_domain())
75 return;
76
77 /* Make sure there's only one xenpv device. */
78 if (devclass_get_device(devclass_find(driver->name), 0))
79 return;
80
81 /*
82 * The xenpv bus should be the last to attach in order
83 * to properly detect if an ISA bus has already been added.
84 */
85 if (BUS_ADD_CHILD(parent, UINT_MAX, driver->name, 0) == NULL)
86 panic("Unable to attach xenpv bus.");
87 }
88
89 static int
90 xenpv_probe(device_t dev)
91 {
92
93 device_set_desc(dev, "Xen PV bus");
94 return (BUS_PROBE_NOWILDCARD);
95 }
96
97 static int
98 xenpv_attach(device_t dev)
99 {
100 int error;
101
102 /*
103 * Let our child drivers identify any child devices that they
104 * can find. Once that is done attach any devices that we
105 * found.
106 */
107 error = bus_generic_probe(dev);
108 if (error)
109 return (error);
110
111 error = bus_generic_attach(dev);
112
113 return (error);
114 }
115
116 static struct resource *
117 xenpv_alloc_physmem(device_t dev, device_t child, int *res_id, size_t size)
118 {
119 struct resource *res;
120 vm_paddr_t phys_addr;
121 void *virt_addr;
122 int error;
123
124 res = bus_alloc_resource(child, SYS_RES_MEMORY, res_id, LOW_MEM_LIMIT,
125 ~0, size, RF_ACTIVE | RF_UNMAPPED);
126 if (res == NULL)
127 return (NULL);
128
129 phys_addr = rman_get_start(res);
130 error = vm_phys_fictitious_reg_range(phys_addr, phys_addr + size,
131 VM_MEMATTR_XEN);
132 if (error) {
133 bus_release_resource(child, SYS_RES_MEMORY, *res_id, res);
134 return (NULL);
135 }
136 virt_addr = pmap_mapdev_attr(phys_addr, size, VM_MEMATTR_XEN);
137 KASSERT(virt_addr != NULL, ("Failed to create linear mappings"));
138 rman_set_virtual(res, virt_addr);
139
140 return (res);
141 }
142
143 static int
144 xenpv_free_physmem(device_t dev, device_t child, int res_id, struct resource *res)
145 {
146 vm_paddr_t phys_addr;
147 void *virt_addr;
148 size_t size;
149
150 phys_addr = rman_get_start(res);
151 size = rman_get_size(res);
152 virt_addr = rman_get_virtual(res);
153
154 pmap_unmapdev(virt_addr, size);
155 vm_phys_fictitious_unreg_range(phys_addr, phys_addr + size);
156 return (bus_release_resource(child, SYS_RES_MEMORY, res_id, res));
157 }
158
159 static device_method_t xenpv_methods[] = {
160 /* Device interface */
161 DEVMETHOD(device_identify, xenpv_identify),
162 DEVMETHOD(device_probe, xenpv_probe),
163 DEVMETHOD(device_attach, xenpv_attach),
164 DEVMETHOD(device_suspend, bus_generic_suspend),
165 DEVMETHOD(device_resume, bus_generic_resume),
166
167 /* Bus interface */
168 DEVMETHOD(bus_add_child, bus_generic_add_child),
169 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
170 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
171 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
172 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
173
174 /* Interface to allocate memory for foreign mappings */
175 DEVMETHOD(xenmem_alloc, xenpv_alloc_physmem),
176 DEVMETHOD(xenmem_free, xenpv_free_physmem),
177
178 DEVMETHOD_END
179 };
180
181 static driver_t xenpv_driver = {
182 "xenpv",
183 xenpv_methods,
184 0,
185 };
186
187 DRIVER_MODULE(xenpv, nexus, xenpv_driver, 0, 0);
188
189 struct resource *
190 xenmem_alloc(device_t dev, int *res_id, size_t size)
191 {
192 device_t parent;
193
194 parent = device_get_parent(dev);
195 if (parent == NULL)
196 return (NULL);
197 return (XENMEM_ALLOC(parent, dev, res_id, size));
198 }
199
200 int
201 xenmem_free(device_t dev, int res_id, struct resource *res)
202 {
203 device_t parent;
204
205 parent = device_get_parent(dev);
206 if (parent == NULL)
207 return (ENXIO);
208 return (XENMEM_FREE(parent, dev, res_id, res));
209 }
Cache object: edbe958e569ca2f900012df59c02bf62
|