xref: /freebsd/sys/dev/xen/bus/xenpv.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/pcpu.h>
34 #include <sys/rman.h>
35 #include <sys/smp.h>
36 #include <sys/limits.h>
37 #include <sys/vmmeter.h>
38 
39 #include <vm/vm.h>
40 #include <vm/vm_page.h>
41 #include <vm/vm_param.h>
42 #include <vm/vm_phys.h>
43 
44 #include <xen/xen-os.h>
45 #include <xen/gnttab.h>
46 
47 #include "xenmem_if.h"
48 
49 /*
50  * Allocate unused physical memory above 4GB in order to map memory
51  * from foreign domains. We use memory starting at 4GB in order to
52  * prevent clashes with MMIO/ACPI regions.
53  *
54  * Since this is not possible on i386 just use any available memory
55  * chunk above 1MB and hope we don't clash with anything else.
56  *
57  * Other architectures better document MMIO regions and drivers more
58  * reliably reserve them.  As such, allow using any unpopulated memory
59  * region.
60  */
61 #ifdef __amd64__
62 #define LOW_MEM_LIMIT	0x100000000ul
63 #elif defined(__i386__)
64 #define LOW_MEM_LIMIT	0x100000ul
65 #else
66 #define LOW_MEM_LIMIT	0
67 #endif
68 
69 static void
70 xenpv_identify(driver_t *driver, device_t parent)
71 {
72 	if (!xen_domain())
73 		return;
74 
75 	/* Make sure there's only one xenpv device. */
76 	if (devclass_get_device(devclass_find(driver->name), 0))
77 		return;
78 
79 	/*
80 	 * The xenpv bus should be the last to attach in order
81 	 * to properly detect if an ISA bus has already been added.
82 	 */
83 	if (BUS_ADD_CHILD(parent, UINT_MAX, driver->name, 0) == NULL)
84 		panic("Unable to attach xenpv bus.");
85 }
86 
87 static int
88 xenpv_probe(device_t dev)
89 {
90 
91 	device_set_desc(dev, "Xen PV bus");
92 	return (BUS_PROBE_NOWILDCARD);
93 }
94 
95 static int
96 xenpv_attach(device_t dev)
97 {
98 	int error;
99 
100 	/*
101 	 * Let our child drivers identify any child devices that they
102 	 * can find.  Once that is done attach any devices that we
103 	 * found.
104 	 */
105 	error = bus_generic_probe(dev);
106 	if (error)
107 		return (error);
108 
109 	error = bus_generic_attach(dev);
110 
111 	return (error);
112 }
113 
114 static struct resource *
115 xenpv_alloc_physmem(device_t dev, device_t child, int *res_id, size_t size)
116 {
117 	struct resource *res;
118 	vm_paddr_t phys_addr;
119 	void *virt_addr;
120 	int error;
121 
122 	res = bus_alloc_resource(child, SYS_RES_MEMORY, res_id, LOW_MEM_LIMIT,
123 	    ~0, size, RF_ACTIVE | RF_UNMAPPED);
124 	if (res == NULL)
125 		return (NULL);
126 
127 	phys_addr = rman_get_start(res);
128 	error = vm_phys_fictitious_reg_range(phys_addr, phys_addr + size,
129 	    VM_MEMATTR_XEN);
130 	if (error) {
131 		bus_release_resource(child, SYS_RES_MEMORY, *res_id, res);
132 		return (NULL);
133 	}
134 	virt_addr = pmap_mapdev_attr(phys_addr, size, VM_MEMATTR_XEN);
135 	KASSERT(virt_addr != NULL, ("Failed to create linear mappings"));
136 	rman_set_virtual(res, virt_addr);
137 
138 	return (res);
139 }
140 
141 static int
142 xenpv_free_physmem(device_t dev, device_t child, int res_id, struct resource *res)
143 {
144 	vm_paddr_t phys_addr;
145 	void *virt_addr;
146 	size_t size;
147 
148 	phys_addr = rman_get_start(res);
149 	size = rman_get_size(res);
150 	virt_addr = rman_get_virtual(res);
151 
152 	pmap_unmapdev(virt_addr, size);
153 	vm_phys_fictitious_unreg_range(phys_addr, phys_addr + size);
154 	return (bus_release_resource(child, SYS_RES_MEMORY, res_id, res));
155 }
156 
157 static device_method_t xenpv_methods[] = {
158 	/* Device interface */
159 	DEVMETHOD(device_identify,		xenpv_identify),
160 	DEVMETHOD(device_probe,			xenpv_probe),
161 	DEVMETHOD(device_attach,		xenpv_attach),
162 	DEVMETHOD(device_suspend,		bus_generic_suspend),
163 	DEVMETHOD(device_resume,		bus_generic_resume),
164 
165 	/* Bus interface */
166 	DEVMETHOD(bus_add_child,		bus_generic_add_child),
167 	DEVMETHOD(bus_alloc_resource,		bus_generic_alloc_resource),
168 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
169 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
170 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
171 
172 	/* Interface to allocate memory for foreign mappings */
173 	DEVMETHOD(xenmem_alloc,			xenpv_alloc_physmem),
174 	DEVMETHOD(xenmem_free,			xenpv_free_physmem),
175 
176 	DEVMETHOD_END
177 };
178 
179 static driver_t xenpv_driver = {
180 	"xenpv",
181 	xenpv_methods,
182 	0,
183 };
184 
185 DRIVER_MODULE(xenpv, nexus, xenpv_driver, 0, 0);
186 
187 struct resource *
188 xenmem_alloc(device_t dev, int *res_id, size_t size)
189 {
190 	device_t parent;
191 
192 	parent = device_get_parent(dev);
193 	if (parent == NULL)
194 		return (NULL);
195 	return (XENMEM_ALLOC(parent, dev, res_id, size));
196 }
197 
198 int
199 xenmem_free(device_t dev, int res_id, struct resource *res)
200 {
201 	device_t parent;
202 
203 	parent = device_get_parent(dev);
204 	if (parent == NULL)
205 		return (ENXIO);
206 	return (XENMEM_FREE(parent, dev, res_id, res));
207 }
208