xref: /freebsd/sys/dev/xen/bus/xenpv.c (revision f929eb1ed50f079f1638cdabe511ac2e347f541a)
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 void
72a48f7ba4SJulien Grall xenpv_identify(driver_t *driver, device_t parent)
73a48f7ba4SJulien Grall {
74a48f7ba4SJulien Grall 	if (!xen_domain())
75a48f7ba4SJulien Grall 		return;
76a48f7ba4SJulien Grall 
77a48f7ba4SJulien Grall 	/* Make sure there's only one xenpv device. */
78651a887fSJohn Baldwin 	if (devclass_get_device(devclass_find(driver->name), 0))
79a48f7ba4SJulien Grall 		return;
80a48f7ba4SJulien Grall 
81a48f7ba4SJulien Grall 	/*
82a48f7ba4SJulien Grall 	 * The xenpv bus should be the last to attach in order
83a48f7ba4SJulien Grall 	 * to properly detect if an ISA bus has already been added.
84a48f7ba4SJulien Grall 	 */
85651a887fSJohn Baldwin 	if (BUS_ADD_CHILD(parent, UINT_MAX, driver->name, 0) == NULL)
86a48f7ba4SJulien Grall 		panic("Unable to attach xenpv bus.");
87a48f7ba4SJulien Grall }
88a48f7ba4SJulien Grall 
89a48f7ba4SJulien Grall static int
90a48f7ba4SJulien Grall xenpv_probe(device_t dev)
91a48f7ba4SJulien Grall {
92a48f7ba4SJulien Grall 
93a48f7ba4SJulien Grall 	device_set_desc(dev, "Xen PV bus");
94a48f7ba4SJulien Grall 	return (BUS_PROBE_NOWILDCARD);
95a48f7ba4SJulien Grall }
96a48f7ba4SJulien Grall 
97a48f7ba4SJulien Grall static int
98a48f7ba4SJulien Grall xenpv_attach(device_t dev)
99a48f7ba4SJulien Grall {
100a48f7ba4SJulien Grall 	int error;
101a48f7ba4SJulien Grall 
102a48f7ba4SJulien Grall 	/*
103a48f7ba4SJulien Grall 	 * Let our child drivers identify any child devices that they
104a48f7ba4SJulien Grall 	 * can find.  Once that is done attach any devices that we
105a48f7ba4SJulien Grall 	 * found.
106a48f7ba4SJulien Grall 	 */
107a48f7ba4SJulien Grall 	error = bus_generic_probe(dev);
108a48f7ba4SJulien Grall 	if (error)
109a48f7ba4SJulien Grall 		return (error);
110a48f7ba4SJulien Grall 
111a48f7ba4SJulien Grall 	error = bus_generic_attach(dev);
112a48f7ba4SJulien Grall 
113a48f7ba4SJulien Grall 	return (error);
114a48f7ba4SJulien Grall }
115a48f7ba4SJulien Grall 
116a48f7ba4SJulien Grall static struct resource *
117a48f7ba4SJulien Grall xenpv_alloc_physmem(device_t dev, device_t child, int *res_id, size_t size)
118a48f7ba4SJulien Grall {
119a48f7ba4SJulien Grall 	struct resource *res;
120a48f7ba4SJulien Grall 	vm_paddr_t phys_addr;
121f4c6843eSRoger Pau Monné 	void *virt_addr;
122a48f7ba4SJulien Grall 	int error;
123a48f7ba4SJulien Grall 
124a48f7ba4SJulien Grall 	res = bus_alloc_resource(child, SYS_RES_MEMORY, res_id, LOW_MEM_LIMIT,
125f4c6843eSRoger Pau Monné 	    ~0, size, RF_ACTIVE | RF_UNMAPPED);
126a48f7ba4SJulien Grall 	if (res == NULL)
127a48f7ba4SJulien Grall 		return (NULL);
128a48f7ba4SJulien Grall 
129a48f7ba4SJulien Grall 	phys_addr = rman_get_start(res);
130a48f7ba4SJulien Grall 	error = vm_phys_fictitious_reg_range(phys_addr, phys_addr + size,
131b6ff9345SElliott Mitchell 	    VM_MEMATTR_XEN);
132a48f7ba4SJulien Grall 	if (error) {
133a48f7ba4SJulien Grall 		bus_release_resource(child, SYS_RES_MEMORY, *res_id, res);
134a48f7ba4SJulien Grall 		return (NULL);
135a48f7ba4SJulien Grall 	}
136f4c6843eSRoger Pau Monné 	virt_addr = pmap_mapdev_attr(phys_addr, size, VM_MEMATTR_XEN);
137f4c6843eSRoger Pau Monné 	KASSERT(virt_addr != NULL, ("Failed to create linear mappings"));
138f4c6843eSRoger Pau Monné 	rman_set_virtual(res, virt_addr);
139a48f7ba4SJulien Grall 
140a48f7ba4SJulien Grall 	return (res);
141a48f7ba4SJulien Grall }
142a48f7ba4SJulien Grall 
143a48f7ba4SJulien Grall static int
144a48f7ba4SJulien Grall xenpv_free_physmem(device_t dev, device_t child, int res_id, struct resource *res)
145a48f7ba4SJulien Grall {
146a48f7ba4SJulien Grall 	vm_paddr_t phys_addr;
147f4c6843eSRoger Pau Monné 	vm_offset_t virt_addr;
148a48f7ba4SJulien Grall 	size_t size;
149a48f7ba4SJulien Grall 
150a48f7ba4SJulien Grall 	phys_addr = rman_get_start(res);
151a48f7ba4SJulien Grall 	size = rman_get_size(res);
152f4c6843eSRoger Pau Monné 	virt_addr = (vm_offset_t)rman_get_virtual(res);
153a48f7ba4SJulien Grall 
154f4c6843eSRoger Pau Monné 	pmap_unmapdev(virt_addr, size);
155a48f7ba4SJulien Grall 	vm_phys_fictitious_unreg_range(phys_addr, phys_addr + size);
156a48f7ba4SJulien Grall 	return (bus_release_resource(child, SYS_RES_MEMORY, res_id, res));
157a48f7ba4SJulien Grall }
158a48f7ba4SJulien Grall 
159a48f7ba4SJulien Grall static device_method_t xenpv_methods[] = {
160a48f7ba4SJulien Grall 	/* Device interface */
161a48f7ba4SJulien Grall 	DEVMETHOD(device_identify,		xenpv_identify),
162a48f7ba4SJulien Grall 	DEVMETHOD(device_probe,			xenpv_probe),
163a48f7ba4SJulien Grall 	DEVMETHOD(device_attach,		xenpv_attach),
164a48f7ba4SJulien Grall 	DEVMETHOD(device_suspend,		bus_generic_suspend),
165a48f7ba4SJulien Grall 	DEVMETHOD(device_resume,		bus_generic_resume),
166a48f7ba4SJulien Grall 
167a48f7ba4SJulien Grall 	/* Bus interface */
168a48f7ba4SJulien Grall 	DEVMETHOD(bus_add_child,		bus_generic_add_child),
169a48f7ba4SJulien Grall 	DEVMETHOD(bus_alloc_resource,		bus_generic_alloc_resource),
170a48f7ba4SJulien Grall 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
171a48f7ba4SJulien Grall 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
172a48f7ba4SJulien Grall 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
173a48f7ba4SJulien Grall 
174a48f7ba4SJulien Grall 	/* Interface to allocate memory for foreign mappings */
175a48f7ba4SJulien Grall 	DEVMETHOD(xenmem_alloc,			xenpv_alloc_physmem),
176a48f7ba4SJulien Grall 	DEVMETHOD(xenmem_free,			xenpv_free_physmem),
177a48f7ba4SJulien Grall 
178a48f7ba4SJulien Grall 	DEVMETHOD_END
179a48f7ba4SJulien Grall };
180a48f7ba4SJulien Grall 
181a48f7ba4SJulien Grall static driver_t xenpv_driver = {
182a48f7ba4SJulien Grall 	"xenpv",
183a48f7ba4SJulien Grall 	xenpv_methods,
184a48f7ba4SJulien Grall 	0,
185a48f7ba4SJulien Grall };
186a48f7ba4SJulien Grall 
187*f929eb1eSJohn Baldwin DRIVER_MODULE(xenpv, nexus, xenpv_driver, 0, 0);
188a48f7ba4SJulien Grall 
189a48f7ba4SJulien Grall struct resource *
190a48f7ba4SJulien Grall xenmem_alloc(device_t dev, int *res_id, size_t size)
191a48f7ba4SJulien Grall {
192a48f7ba4SJulien Grall 	device_t parent;
193a48f7ba4SJulien Grall 
194a48f7ba4SJulien Grall 	parent = device_get_parent(dev);
195a48f7ba4SJulien Grall 	if (parent == NULL)
196a48f7ba4SJulien Grall 		return (NULL);
197a48f7ba4SJulien Grall 	return (XENMEM_ALLOC(parent, dev, res_id, size));
198a48f7ba4SJulien Grall }
199a48f7ba4SJulien Grall 
200a48f7ba4SJulien Grall int
201a48f7ba4SJulien Grall xenmem_free(device_t dev, int res_id, struct resource *res)
202a48f7ba4SJulien Grall {
203a48f7ba4SJulien Grall 	device_t parent;
204a48f7ba4SJulien Grall 
205a48f7ba4SJulien Grall 	parent = device_get_parent(dev);
206a48f7ba4SJulien Grall 	if (parent == NULL)
207a48f7ba4SJulien Grall 		return (ENXIO);
208a48f7ba4SJulien Grall 	return (XENMEM_FREE(parent, dev, res_id, res));
209a48f7ba4SJulien Grall }
210