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