xref: /freebsd/sys/dev/xen/bus/xenpv.c (revision 651a887f4e66b15145508e74db72ebb73c22112b)
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.
58e627e25dSElliott Mitchell  *
59e627e25dSElliott Mitchell  * Other architectures better document MMIO regions and drivers more
60e627e25dSElliott Mitchell  * reliably reserve them.  As such, allow using any unpopulated memory
61e627e25dSElliott 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
68e627e25dSElliott 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. */
80*651a887fSJohn Baldwin 	if (devclass_get_device(devclass_find(driver->name), 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 	 */
87*651a887fSJohn Baldwin 	if (BUS_ADD_CHILD(parent, UINT_MAX, driver->name, 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;
123f4c6843eSRoger Pau Monné 	void *virt_addr;
124a48f7ba4SJulien Grall 	int error;
125a48f7ba4SJulien Grall 
126a48f7ba4SJulien Grall 	res = bus_alloc_resource(child, SYS_RES_MEMORY, res_id, LOW_MEM_LIMIT,
127f4c6843eSRoger Pau Monné 	    ~0, size, RF_ACTIVE | RF_UNMAPPED);
128a48f7ba4SJulien Grall 	if (res == NULL)
129a48f7ba4SJulien Grall 		return (NULL);
130a48f7ba4SJulien Grall 
131a48f7ba4SJulien Grall 	phys_addr = rman_get_start(res);
132a48f7ba4SJulien Grall 	error = vm_phys_fictitious_reg_range(phys_addr, phys_addr + size,
133b6ff9345SElliott Mitchell 	    VM_MEMATTR_XEN);
134a48f7ba4SJulien Grall 	if (error) {
135a48f7ba4SJulien Grall 		bus_release_resource(child, SYS_RES_MEMORY, *res_id, res);
136a48f7ba4SJulien Grall 		return (NULL);
137a48f7ba4SJulien Grall 	}
138f4c6843eSRoger Pau Monné 	virt_addr = pmap_mapdev_attr(phys_addr, size, VM_MEMATTR_XEN);
139f4c6843eSRoger Pau Monné 	KASSERT(virt_addr != NULL, ("Failed to create linear mappings"));
140f4c6843eSRoger Pau Monné 	rman_set_virtual(res, virt_addr);
141a48f7ba4SJulien Grall 
142a48f7ba4SJulien Grall 	return (res);
143a48f7ba4SJulien Grall }
144a48f7ba4SJulien Grall 
145a48f7ba4SJulien Grall static int
146a48f7ba4SJulien Grall xenpv_free_physmem(device_t dev, device_t child, int res_id, struct resource *res)
147a48f7ba4SJulien Grall {
148a48f7ba4SJulien Grall 	vm_paddr_t phys_addr;
149f4c6843eSRoger Pau Monné 	vm_offset_t virt_addr;
150a48f7ba4SJulien Grall 	size_t size;
151a48f7ba4SJulien Grall 
152a48f7ba4SJulien Grall 	phys_addr = rman_get_start(res);
153a48f7ba4SJulien Grall 	size = rman_get_size(res);
154f4c6843eSRoger Pau Monné 	virt_addr = (vm_offset_t)rman_get_virtual(res);
155a48f7ba4SJulien Grall 
156f4c6843eSRoger Pau Monné 	pmap_unmapdev(virt_addr, size);
157a48f7ba4SJulien Grall 	vm_phys_fictitious_unreg_range(phys_addr, phys_addr + size);
158a48f7ba4SJulien Grall 	return (bus_release_resource(child, SYS_RES_MEMORY, res_id, res));
159a48f7ba4SJulien Grall }
160a48f7ba4SJulien Grall 
161a48f7ba4SJulien Grall static device_method_t xenpv_methods[] = {
162a48f7ba4SJulien Grall 	/* Device interface */
163a48f7ba4SJulien Grall 	DEVMETHOD(device_identify,		xenpv_identify),
164a48f7ba4SJulien Grall 	DEVMETHOD(device_probe,			xenpv_probe),
165a48f7ba4SJulien Grall 	DEVMETHOD(device_attach,		xenpv_attach),
166a48f7ba4SJulien Grall 	DEVMETHOD(device_suspend,		bus_generic_suspend),
167a48f7ba4SJulien Grall 	DEVMETHOD(device_resume,		bus_generic_resume),
168a48f7ba4SJulien Grall 
169a48f7ba4SJulien Grall 	/* Bus interface */
170a48f7ba4SJulien Grall 	DEVMETHOD(bus_add_child,		bus_generic_add_child),
171a48f7ba4SJulien Grall 	DEVMETHOD(bus_alloc_resource,		bus_generic_alloc_resource),
172a48f7ba4SJulien Grall 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
173a48f7ba4SJulien Grall 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
174a48f7ba4SJulien Grall 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
175a48f7ba4SJulien Grall 
176a48f7ba4SJulien Grall 	/* Interface to allocate memory for foreign mappings */
177a48f7ba4SJulien Grall 	DEVMETHOD(xenmem_alloc,			xenpv_alloc_physmem),
178a48f7ba4SJulien Grall 	DEVMETHOD(xenmem_free,			xenpv_free_physmem),
179a48f7ba4SJulien Grall 
180a48f7ba4SJulien Grall 	DEVMETHOD_END
181a48f7ba4SJulien Grall };
182a48f7ba4SJulien Grall 
183a48f7ba4SJulien Grall static driver_t xenpv_driver = {
184a48f7ba4SJulien Grall 	"xenpv",
185a48f7ba4SJulien Grall 	xenpv_methods,
186a48f7ba4SJulien Grall 	0,
187a48f7ba4SJulien Grall };
188a48f7ba4SJulien Grall 
189a48f7ba4SJulien Grall DRIVER_MODULE(xenpv, nexus, xenpv_driver, xenpv_devclass, 0, 0);
190a48f7ba4SJulien Grall 
191a48f7ba4SJulien Grall struct resource *
192a48f7ba4SJulien Grall xenmem_alloc(device_t dev, int *res_id, size_t size)
193a48f7ba4SJulien Grall {
194a48f7ba4SJulien Grall 	device_t parent;
195a48f7ba4SJulien Grall 
196a48f7ba4SJulien Grall 	parent = device_get_parent(dev);
197a48f7ba4SJulien Grall 	if (parent == NULL)
198a48f7ba4SJulien Grall 		return (NULL);
199a48f7ba4SJulien Grall 	return (XENMEM_ALLOC(parent, dev, res_id, size));
200a48f7ba4SJulien Grall }
201a48f7ba4SJulien Grall 
202a48f7ba4SJulien Grall int
203a48f7ba4SJulien Grall xenmem_free(device_t dev, int res_id, struct resource *res)
204a48f7ba4SJulien Grall {
205a48f7ba4SJulien Grall 	device_t parent;
206a48f7ba4SJulien Grall 
207a48f7ba4SJulien Grall 	parent = device_get_parent(dev);
208a48f7ba4SJulien Grall 	if (parent == NULL)
209a48f7ba4SJulien Grall 		return (ENXIO);
210a48f7ba4SJulien Grall 	return (XENMEM_FREE(parent, dev, res_id, res));
211a48f7ba4SJulien Grall }
212