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